1.0.6 • Published 2 years ago

@appyhigh/express-error-handler v1.0.6

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

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

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.ACCEPTEDStringAccepted
httpCode.BAD_GATEWAYStringBad Gateway
httpCode.BAD_REQUESTStringBad Request
httpCode.CONFLICTStringConflict
httpCode.CONTINUEStringContinue
httpCode.CREATEDStringCreated
httpCode.EXPECTATION_FAILEDStringExpectation Failed
httpCode.FAILED_DEPENDENCYStringFailed Dependency
httpCode.FORBIDDENStringForbidden
httpCode.GATEWAY_TIMEOUTStringGateway Timeout
httpCode.GONEStringGone
httpCode.HTTP_VERSION_NOT_SUPPORTEDStringHTTP Version Not Supported
httpCode.IM_A_TEAPOTStringI\'m a teapot
httpCode.INSUFFICIENT_SPACE_ON_RESOURCEStringInsufficient Space on Resource
httpCode.INSUFFICIENT_STORAGEStringInsufficient Storage
httpCode.INTERNAL_SERVER_ERRORStringServer Error
httpCode.LENGTH_REQUIREDStringLength Required
httpCode.LOCKEDStringLocked
httpCode.METHOD_FAILUREStringMethod Failure
httpCode.METHOD_NOT_ALLOWEDStringMethod Not Allowed
httpCode.MOVED_PERMANENTLYStringMoved Permanently
httpCode.MOVED_TEMPORARILYStringMoved Temporarily
httpCode.MULTI_STATUSStringMulti-Status
httpCode.MULTIPLE_CHOICESStringMultiple Choices
httpCode.NETWORK_AUTHENTICATION_REQUIREDStringNetwork Authentication Required
httpCode.NO_CONTENTStringNo Content
httpCode.NON_AUTHORITATIVE_INFORMATIONStringNon Authoritative Information
httpCode.NOT_ACCEPTABLEStringNot Acceptable
httpCode.NOT_FOUNDStringNot Found
httpCode.NOT_IMPLEMENTEDStringNot Implemented
httpCode.NOT_MODIFIEDStringNot Modified
httpCode.OK_REQUESTStringOk Request
httpCode.PARTIAL_CONTENTStringPartial Content
httpCode.PAYMENT_REQUIREDStringPayment Required
httpCode.PERMANENT_REDIRECTStringPermanent Redirect
httpCode.PRECONDITION_FAILEDStringPrecondition Failed
httpCode.PRECONDITION_REQUIREDStringPrecondition Required
httpCode.PROCESSINGStringProcessing
httpCode.PROXY_AUTHENTICATION_REQUIREDStringProxy Authentication Required
httpCode.REQUEST_HEADER_FIELDS_TOO_LARGEStringRequest Header Fields Too Large
httpCode.REQUEST_TIMEOUTStringRequest Timeout
httpCode.REQUEST_TOO_LONGStringRequest Entity Too Large
httpCode.REQUEST_URI_TOO_LONGStringRequest-URI Too Long
httpCode.REQUESTED_RANGE_NOT_SATISFIABLEStringRequested Range Not Satisfiable
httpCode.RESET_CONTENTStringReset Content
httpCode.SEE_OTHERStringSee Other
httpCode.SERVICE_UNAVAILABLEStringService Unavailable
httpCode.SWITCHING_PROTOCOLSStringSwitching Protocols
httpCode.TEMPORARY_REDIRECTStringTemporary Redirect
httpCode.TOO_MANY_REQUESTSStringToo Many Requests
httpCode.UNAUTHORIZEDStringUnauthorized
httpCode.UNPROCESSABLE_ENTITYStringUnprocessable Entity
httpCode.UNSUPPORTED_MEDIA_TYPEStringUnsupported Media Type
httpCode.USE_PROXYStringUse Proxy

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

MIT

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