1.0.6 • Published 2 years ago
@appyhigh/express-error-handler v1.0.6
express-error-handler
Installation
$ npm install @appyhigh/express-error-handler --save
Use
Check here for sample application
Methods
expressErrorHandler(options: Object) => (error, request, response, next)
This is a function which will work as a middleware for your express app so that your errors will response with an HTTP response. if you will pass database URL then your errors will be saved in database for future analytics.
You have to pass options objects as parameters (*options is optional*).
Example
const express = require('express');
const { expressErrorHandler } = require('@appyhigh/express-error-handler');
const app = express();
app.use(expressErrorHandler({
environment : 'development',
dbUrl: 'mongodb+srv://<username>:<password>@clustername.mongodb.net/test?retryWrites=true&w=majority&useNewUrlParser=true&useUnifiedTopology=true',
dbName: 'test-1',
appName: 'testing-1',
errorLogs : false,
trace : true,
errorDescription : true,
errorOrigin : true
}));
Options
Option | Type | Default | Description |
---|---|---|---|
environment | String | development | it can be either development or production . |
trace | Boolean | true | If true the trace is attached to response. |
errorLogs | Boolean | false | If true all errors are printed via console.error . |
errorDescription | Boolean | true | If true then error message for developer will be attached to response. |
errorOrigin | Boolean | true | If true then error origin place in your code will be attached to response. |
dbUrl | String | | If database string given then errors will be save in database. | |
dbName | String | errorApp | Save the errors in given database name. |
appName | String | errorApp | This property will help to group the errors application wise in database collection. |
Example
5xx error trace: true
:
{
"statusCode": 404,
"error": {
"stack": "Error\n at new AppError (/home/akumar/instore-dev/insta-downloader-backend/lib/errorClasses/appError.js:11:5)\n at next (/home/akumar/instore-dev/insta-downloader-backend/node_modules/express/lib/router/index.js:260:14)",
"errorDescription": "Error info for developer",
"type": "Error_origin",
"errorUserTitle": "Error title for user",
"errorUserMsg": "Error message for user"
}
}
Definition of a "Error"
The error could contain the following fields:
Error Key | Purpose |
---|---|
stack | Trace including data for dubug such as file, paths. |
errorDescription | Error message for developer debug purpose. |
type | Error origin means in which module error has occured. |
errorUserTitle | Error title for user popup. |
errorUserMsg | Error message for user popup. |
statusCode | HTTP status code for response. Default value: 500 (Internal Server Error). |
HTTP Code
Name | Type | value |
---|---|---|
ACCEPTED | Number | 202 |
BAD_GATEWAY | Number | 502 |
BAD_REQUEST | Number | 400 |
CONFLICT | Number | 409 |
CONTINUE | Number | 100 |
CREATED | Number | 201 |
EXPECTATION_FAILED | Number | 417 |
FAILED_DEPENDENCY | Number | 424 |
FORBIDDEN | Number | 403 |
GATEWAY_TIMEOUT | Number | 504 |
GONE | Number | 410 |
HTTP_VERSION_NOT_SUPPORTED | Number | 505 |
IM_A_TEAPOT | Number | 418 |
INSUFFICIENT_SPACE_ON_RESOURCE | Number | 419 |
INSUFFICIENT_STORAGE | Number | 507 |
INTERNAL_SERVER_ERROR | Number | 500 |
LENGTH_REQUIRED | Number | 411 |
LOCKED | Number | 423 |
METHOD_FAILURE | Number | 420 |
METHOD_NOT_ALLOWED | Number | 405 |
MOVED_PERMANENTLY | Number | 301 |
MOVED_TEMPORARILY | Number | 302 |
MULTI_STATUS | Number | 207 |
MULTIPLE_CHOICES | Number | 300 |
NETWORK_AUTHENTICATION_REQUIRED | Number | 511 |
NO_CONTENT | Number | 204 |
NON_AUTHORITATIVE_INFORMATION | Number | 203 |
NOT_ACCEPTABLE | Number | 406 |
NOT_FOUND | Number | 404 |
NOT_IMPLEMENTED | Number | 501 |
NOT_MODIFIED | Number | 304 |
OK_REQUEST | Number | 200 |
PARTIAL_CONTENT | Number | 206 |
PAYMENT_REQUIRED | Number | 402 |
PERMANENT_REDIRECT | Number | 308 |
PRECONDITION_FAILED | Number | 412 |
PRECONDITION_REQUIRED | Number | 428 |
PROCESSING | Number | 102 |
PROXY_AUTHENTICATION_REQUIRED | Number | 407 |
REQUEST_HEADER_FIELDS_TOO_LARGE | Number | 431 |
REQUEST_TIMEOUT | Number | 408 |
REQUEST_TOO_LONG | Number | 413 |
REQUEST_URI_TOO_LONG | Number | 414 |
REQUESTED_RANGE_NOT_SATISFIABLE | Number | 416 |
RESET_CONTENT | Number | 205 |
SEE_OTHER | Number | 303 |
SERVICE_UNAVAILABLE | Number | 503 |
SWITCHING_PROTOCOLS | Number | 101 |
TEMPORARY_REDIRECT | Number | 307 |
TOO_MANY_REQUESTS | Number | 429 |
UNAUTHORIZED | Number | 401 |
UNPROCESSABLE_ENTITY | Number | 422 |
UNSUPPORTED_MEDIA_TYPE | Number | 415 |
USE_PROXY | Number | 305 |
HTTP Code Description
Example
const { AppError, httpCode, httpMessage } = require('@appyhigh/express-error-handler');
const knownError = {
errorDescription: 'Error description for developer.',
type: 'error origin place or module name',
errorUserTitle: 'Error title for user',
errorUserMsg: httpMessage[httpCode.INTERNAL_SERVER_ERROR],
httpCode: httpCode.INTERNAL_SERVER_ERROR
};
try{
//do something....
}
catch(error){
throw new AppError(knownError,{});
}
License
2.0.0-beta0
2 years ago
2.0.0-beta.2
2 years ago
2.0.0-beta.1
2 years ago
2.0.0-beta.0
2 years ago
2.0.0-beta.4
2 years ago
2.0.0-beta.3
2 years ago
1.0.6
2 years ago
1.0.5
2 years ago
1.0.4
2 years ago
1.0.3
2 years ago
1.0.2
2 years ago
1.0.1
2 years ago
1.0.0
2 years ago