@kudobuzz/express-error
v2.0.0
Published
This is a express error module
Downloads
553
Readme
express-errors
A bunch of utilities to create errors and handle errors
Installation
npm i @kudobuzz/express-error
Api
customError
Options
const validationError = customError({name,message,code})
| Parameter | Description | isRequired |
| ------------- | ------------- | ------- |
|name | Error name | YES
|
|message | Error Message | YES
|
|code | Error code | NO
|
Usage
const {customError} = require('@kudobuzz/express-error'
const name = 'ValidationError'
const message = 'parameter is required'
const code = 'ERR_VALIDATION_ERROR'
const validationError = customError({name,message, codeee}))
try {
throw ValidationError
} catch(error) {
console.log(customError.getMessage(validationError)) // to get message
console.log(customError.getCode(validationError)) // to get code
console.log(customError.getName(validationError)) // to get name
}
validationError
Options
const validationError = validationError({name, message, code, details})
| Parameter | Description | isRequired |
| ------------- | ------------- | ------- |
|name | Error name | YES
|
|message | Error Message | YES
|
|code | Error Message | N0
|details | Error code | YES
|
Usage
const name = 'StripeValidationError'
const message = 'Error validating subscription details'
const details = [{
param: 'subscription_id',
message: 'subscripton_id should be characters only',
value: '34456755454'
}]
const validationError = validationError({name, message, code, details})
try {
throw validationError
} catch(error) {
console.log(customError.getMessage(validationError)) // to get message
console.log(customError.getCode(validationError)) // to get code
console.log(customError.getName(validationError)) // to get name
console.log(validaationError.details)
}
NotFound
Usage
const {notFound} = require('@kudobuzz/express-error')
const app = require('express)()
app.get('/subscription',createSubscription )
app.use(notFound)
Examples
setErrorStatus
This middleware sets statuses for expected error
Usage
const { setErrorStatus } = require('@kudobuzz/express-error')
const httpStatus = require('@kudobuzz/http-status')
const app = require('express')()
const ERR_MAP = {
SubscriptionFailedError: httpStatus.FORBIDDEN
}
app.get('/subscription', createSubscription)
app.use(setErrorStatus(ERR_MAP))
sendError
This middleware is responsible to sending error responses to client. The response object follows the api standard
const { sendError } = require('@kudobuzz/express-error')
const app = require('express')()
app.use(notFound)
app.use(sendError)