Create HTTP error responses
npm install error-http-response






Create HTTP error responses.
This converts errors to plain objects
(RFC 7807, "problem details") to use
in an HTTP response.
The main HTTP response fields are automatically set usingerror.name,error.messageerror.stack
and other error properties.
``jsAuthError: Could not authenticate.
const error = new AuthError('Could not authenticate.')
error.userId = 62
const object = errorHttpResponse(error)
// {
// title: 'AuthError',
// detail: 'Could not authenticate.',
// stack:
// at ...,`
// extra: { userId: 62 }
// }
Additional fields can be explicitly set in the error class's constructor, using
this.http.
`js`
class AuthError extends Error {
constructor(...args) {
super(...args)
this.http = {
type: 'https://example.com/probs/auth',
status: 401,
}
}
}
Or on the error instance, using error.http.
`js`
const error = new AuthError('Could not authenticate.')
Object.assign(error.http, {
instance: '/users/62',
extra: { userId: 62 },
})
Or as an argument.
`js
import errorHttpResponse from 'error-http-response'
const object = errorHttpResponse(error, {
extra: { isHttp: true },
})
// {
// type: 'https://example.com/probs/auth',
// status: 401,
// title: 'AuthError',
// detail: 'Could not authenticate.',
// instance: '/users/62',
// stack: AuthError: Could not authenticate.
// at ...,`
// extra: { isHttp: true, userId: 62 },
// }
`bash`
npm install error-http-response
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.
value Error | any\options Options?\HttpResponse
_Return value_:
Converts error to a plain object
(RFC 7807, "problem details") to use
in an HTTP response.
error should be an Error instance, but invalid errors are automatically
normalized.
_Type_: object
The options and the return value have the same shape
(RFC 7807). Options can be passed
either as an argument to
errorHttpResponse() or be set to
error.http.
Options are validated: an exception is thrown if their syntax is invalid.
_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.
- modern-errors: Handle errors in
a simple, stable, consistent way
- modern-errors-http:
modern-errors plugin to create HTTP error responseserror-custom-class
- : Createerror-class-utils
one error class
- : Utilitieserror-serializer
to properly create error classes
- : Convertnormalize-exception
errors to/from plain objects
- :is-error-instance
Normalize exceptions/errors
- : Check ifError
a value is an instancemerge-error-cause
- : Merge ancause
error with its set-error-class
- : Properlyset-error-message
update an error's class
- : Properlywrap-error-message
update an error's message
- :set-error-props
Properly wrap an error's message
- : Properlyset-error-stack
update an error's properties
- : Properlyerror-cause-polyfill
update an error's stack
- :error.cause
Polyfill handle-cli-error
- : 💣 Errorsafe-json-value
handler for CLI applications 💥
- : ⛑️ JSONlog-process-errors
serialization should never fail
- : Showerror-http-response
some ❤ to Node.js process errors
- :winston-error-format
Create HTTP error responses
- : Log
errors with Winston
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!