`modern-errors` plugin to create HTTP error responses
npm install modern-errors-http







modern-errors
plugin to create HTTP error
responses.
This adds BaseError.httpResponse(error) which
converts error to a plain object
(RFC 7807, "problem details") to use
in an HTTP response.
Adding the plugin tomodern-errors.
``js
import ModernError from 'modern-errors'
import modernErrorsHttp from 'modern-errors-http'
export const BaseError = ModernError.subclass('BaseError', {
plugins: [modernErrorsHttp],
})
`
Configuring error fields.
`js`
export const AuthError = BaseError.subclass('AuthError', {
http: {
type: 'https://example.com/probs/auth',
status: 401,
},
})
Creating an HTTP error response.
`jsAuthError: Could not authenticate.
const error = new AuthError('Could not authenticate.', {
http: {
instance: '/users/62',
extra: { userId: 62 },
},
})
const object = BaseError.httpResponse(error)
// {
// type: 'https://example.com/probs/auth',
// status: 401,
// title: 'AuthError',
// detail: 'Could not authenticate.',
// instance: '/users/62',
// stack:
// at ...,`
// extra: { userId: 62 },
// }
`bash`
npm install modern-errors-http
This package works in both Node.js >=18.18.0 and
browsers.
This is an ES module. It must be loaded using
an import or import() statement,
not require(). If TypeScript is used, it must be configured to
output ES modules,
not CommonJS.
_Type_: Plugin
Plugin object to pass to the
plugins option of
ErrorClass.subclass().
error: Error\HttpResponse
_Return value_:
Converts error to a plain object to use in an HTTP response. Its shape follows
RFC 7807 ("problem details").
_Type_: object
_Type_: urlString\undefined
_Default_:
URI identifying and documenting the error class. Ideally, each error class
should set one.
_Type_: integer\undefined
_Default_:
HTTP status code.
_Type_: string\error.name
_Default_:
Error class name.
_Type_: string\error.message
_Default_:
Error description.
_Type_: urlString\undefined
_Default_:
URI identifying the value which errored.
_Type_: string\error.stack
_Default_:
Error stack trace. Can be set to an empty string.
_Type_: object\error
_Default_: any additional properties
Additional information. This is always
safe to serialize as JSON. Can be
set to an empty object.
Options can apply to (in priority order):
- Any error: second argument to
ModernError.subclass()
`js`
export const BaseError = ModernError.subclass('BaseError', {
plugins: [modernErrorsHttp],
http: options,
})
- Any error of a specific class (and its subclasses): second argument to
ErrorClass.subclass()
`js`
export const AuthError = BaseError.subclass('AuthError', { http: options })
- A specific error: second argument to
new ErrorClass()
`js`
throw new AuthError('...', { http: options })
- A specific BaseError.httpResponse(error) call
`js`
BaseError.httpResponse(error, options)
- error-http-response:
Create HTTP error responses
- safe-json-value: ⛑️ JSON
serialization should never fail
- modern-errors: Handle errors in
a simple, stable, consistent way
- modern-errors-cli: Handle
errors in CLI modules
- modern-errors-process:
Handle process errors
- modern-errors-bugs: Print
where to report bugs
- modern-errors-serialize:
Serialize/parse errors
- modern-errors-clean: Clean
stack traces
- modern-errors-winston:
Log errors with Winston
- modern-errors-switch:
Execute class-specific logic
For any question, _don't hesitate_ to submit an issue on GitHub.
Everyone is welcome regardless of personal background. We enforce a
Code of conduct in order to promote a positive and
inclusive environment.
This project was made with ❤️. The simplest way to give back is by starring and
sharing it online.
If the documentation is unclear or has a typo, please click on the page's Edit`
button (pencil icon) and suggest a correction.
If you would like to help us fix a bug or add a new feature, please check our
guidelines. Pull requests are welcome!