Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

12 Commits

Repository files navigation

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

constexpress=require('express');const{ expressErrorHandler }=require('@appyhigh/express-error-handler');constapp=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

OptionTypeDefaultDescription
environmentStringdevelopmentit can be either development or production.
traceBooleantrueIf true the trace is attached to response.
errorLogsBooleanfalseIf true all errors are printed via console.error.
errorDescriptionBooleantrueIf true then error message for developer will be attached to response.
errorOriginBooleantrueIf true then error origin place in your code will be attached to response.
dbUrlString``If database string given then errors will be save in database.
dbNameStringerrorAppSave the errors in given database name.
appNameStringerrorAppThis 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 KeyPurpose
stackTrace including data for dubug such as file, paths.
errorDescriptionError message for developer debug purpose.
typeError origin means in which module error has occured.
errorUserTitleError title for user popup.
errorUserMsgError message for user popup.
statusCodeHTTP status code for response. Default value: 500 (Internal Server Error).

HTTP Code

NameTypevalue
ACCEPTEDNumber202
BAD_GATEWAYNumber502
BAD_REQUESTNumber400
CONFLICTNumber409
CONTINUENumber100
CREATEDNumber201
EXPECTATION_FAILEDNumber417
FAILED_DEPENDENCYNumber424
FORBIDDENNumber403
GATEWAY_TIMEOUTNumber504
GONENumber410
HTTP_VERSION_NOT_SUPPORTEDNumber505
IM_A_TEAPOTNumber418
INSUFFICIENT_SPACE_ON_RESOURCENumber419
INSUFFICIENT_STORAGENumber507
INTERNAL_SERVER_ERRORNumber500
LENGTH_REQUIREDNumber411
LOCKEDNumber423
METHOD_FAILURENumber420
METHOD_NOT_ALLOWEDNumber405
MOVED_PERMANENTLYNumber301
MOVED_TEMPORARILYNumber302
MULTI_STATUSNumber207
MULTIPLE_CHOICESNumber300
NETWORK_AUTHENTICATION_REQUIREDNumber511
NO_CONTENTNumber204
NON_AUTHORITATIVE_INFORMATIONNumber203
NOT_ACCEPTABLENumber406
NOT_FOUNDNumber404
NOT_IMPLEMENTEDNumber501
NOT_MODIFIEDNumber304
OK_REQUESTNumber200
PARTIAL_CONTENTNumber206
PAYMENT_REQUIREDNumber402
PERMANENT_REDIRECTNumber308
PRECONDITION_FAILEDNumber412
PRECONDITION_REQUIREDNumber428
PROCESSINGNumber102
PROXY_AUTHENTICATION_REQUIREDNumber407
REQUEST_HEADER_FIELDS_TOO_LARGENumber431
REQUEST_TIMEOUTNumber408
REQUEST_TOO_LONGNumber413
REQUEST_URI_TOO_LONGNumber414
REQUESTED_RANGE_NOT_SATISFIABLENumber416
RESET_CONTENTNumber205
SEE_OTHERNumber303
SERVICE_UNAVAILABLENumber503
SWITCHING_PROTOCOLSNumber101
TEMPORARY_REDIRECTNumber307
TOO_MANY_REQUESTSNumber429
UNAUTHORIZEDNumber401
UNPROCESSABLE_ENTITYNumber422
UNSUPPORTED_MEDIA_TYPENumber415
USE_PROXYNumber305

HTTP Code Description

NameTypeDescription
[httpCode.ACCEPTED]StringAccepted
[httpCode.BAD_GATEWAY]StringBad Gateway
[httpCode.BAD_REQUEST]StringBad Request
[httpCode.CONFLICT]StringConflict
[httpCode.CONTINUE]StringContinue
[httpCode.CREATED]StringCreated
[httpCode.EXPECTATION_FAILED]StringExpectation Failed
[httpCode.FAILED_DEPENDENCY]StringFailed Dependency
[httpCode.FORBIDDEN]StringForbidden
[httpCode.GATEWAY_TIMEOUT]StringGateway Timeout
[httpCode.GONE]StringGone
[httpCode.HTTP_VERSION_NOT_SUPPORTED]StringHTTP Version Not Supported
[httpCode.IM_A_TEAPOT]StringI\'m a teapot
[httpCode.INSUFFICIENT_SPACE_ON_RESOURCE]StringInsufficient Space on Resource
[httpCode.INSUFFICIENT_STORAGE]StringInsufficient Storage
[httpCode.INTERNAL_SERVER_ERROR]StringServer Error
[httpCode.LENGTH_REQUIRED]StringLength Required
[httpCode.LOCKED]StringLocked
[httpCode.METHOD_FAILURE]StringMethod Failure
[httpCode.METHOD_NOT_ALLOWED]StringMethod Not Allowed
[httpCode.MOVED_PERMANENTLY]StringMoved Permanently
[httpCode.MOVED_TEMPORARILY]StringMoved Temporarily
[httpCode.MULTI_STATUS]StringMulti-Status
[httpCode.MULTIPLE_CHOICES]StringMultiple Choices
[httpCode.NETWORK_AUTHENTICATION_REQUIRED]StringNetwork Authentication Required
[httpCode.NO_CONTENT]StringNo Content
[httpCode.NON_AUTHORITATIVE_INFORMATION]StringNon Authoritative Information
[httpCode.NOT_ACCEPTABLE]StringNot Acceptable
[httpCode.NOT_FOUND]StringNot Found
[httpCode.NOT_IMPLEMENTED]StringNot Implemented
[httpCode.NOT_MODIFIED]StringNot Modified
[httpCode.OK_REQUEST]StringOk Request
[httpCode.PARTIAL_CONTENT]StringPartial Content
[httpCode.PAYMENT_REQUIRED]StringPayment Required
[httpCode.PERMANENT_REDIRECT]StringPermanent Redirect
[httpCode.PRECONDITION_FAILED]StringPrecondition Failed
[httpCode.PRECONDITION_REQUIRED]StringPrecondition Required
[httpCode.PROCESSING]StringProcessing
[httpCode.PROXY_AUTHENTICATION_REQUIRED]StringProxy Authentication Required
[httpCode.REQUEST_HEADER_FIELDS_TOO_LARGE]StringRequest Header Fields Too Large
[httpCode.REQUEST_TIMEOUT]StringRequest Timeout
[httpCode.REQUEST_TOO_LONG]StringRequest Entity Too Large
[httpCode.REQUEST_URI_TOO_LONG]StringRequest-URI Too Long
[httpCode.REQUESTED_RANGE_NOT_SATISFIABLE]StringRequested Range Not Satisfiable
[httpCode.RESET_CONTENT]StringReset Content
[httpCode.SEE_OTHER]StringSee Other
[httpCode.SERVICE_UNAVAILABLE]StringService Unavailable
[httpCode.SWITCHING_PROTOCOLS]StringSwitching Protocols
[httpCode.TEMPORARY_REDIRECT]StringTemporary Redirect
[httpCode.TOO_MANY_REQUESTS]StringToo Many Requests
[httpCode.UNAUTHORIZED]StringUnauthorized
[httpCode.UNPROCESSABLE_ENTITY]StringUnprocessable Entity
[httpCode.UNSUPPORTED_MEDIA_TYPE]StringUnsupported Media Type
[httpCode.USE_PROXY]StringUse Proxy

Example

const{ AppError, httpCode, httpMessage }=require('@appyhigh/express-error-handler');constknownError={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){thrownewAppError(knownError,{});}

License

MIT

About

No description, website, or topics provided.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages