2.0.4 • Published 1 year ago

@abs-warranty/http-errors v2.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Maintainability Test Coverage

Custom HTTP Errors

To Install:

yarn add @abs-warranty/http-errors

Example Usage:

const { NotFoundError } = require('@abs-warranty/http-errors')

throw new NotFoundError('<yourMessageHere>')

Available Errors:

ErrorStatus CodeMessageDescription
BadReqeustError400bad requestCustom error denoting a bad request was made
UnauthorizedError401unauthorizedCustom error denoting the user is not authorized
ForbiddenError403forbiddenCustom error denoting the user has insufficient privileges
NotFoundError404not foundCustom error denoting a resource has not been found
NotAllowedError405method not allowedCustom error denoting the method used is not allowed for this endpoint
ConflictError409conflictCustom error denoting a resource is in conflict
IAmATeapotError418i am a teapotCustom error denoting that the machine really is actually a teapot 🍵
EnhanceYourCalmError420enhance your calmCustom error denoting the user must enhance their frickin' calm, maaaan. 🌿
UnprocessableEntityError422unprocessable entityCustom error denoting an unprocessable entity
InternalServerError500internal server errorCustom error denoting an internal server error

Error Handler Usage:

// Import and create express app
const express = require('express')
const app = express()
// Import HttpErrors middleware
const { NotFoundError } = require('@abs-warranty/http-errors')
const { errorHandler, notFoundHandler, axiosErrorHandler } = require('@abs-warranty/http-errors/middleware')

// All routes are placed here
app.get('/example', (req, res, next) => {
  throw new NotFoundError('Resource not found')
})

// Handle invalid endpoints
app.use(notFoundHandler)

// Axios error handler converts axios errors to http errors
app.use(axiosErrorHandler)

// Catch-all error handler must be used as last middleware in application
app.use(errorHandler)

Note: These middleware must be placed after all routes have been added to express application

2.0.4

1 year ago

2.0.3

1 year ago

2.0.2

1 year ago

2.0.1

1 year ago

2.0.0

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago