Get the HTTP status code for an error object.
MIT License
Get the HTTP status code for an error object.
This library requires the following to run:
Install with npm:
npm install @rowanmanning/get-error-http-status
Load the library into your code with a require
call:
const getErrorHttpStatus = require('@rowanmanning/get-error-http-status');
Call the method with an error object to get the status code:
const notFoundError = new Error('Not Found');
notFoundError.status = 404;
const status = getErrorHttpStatus(error); // 404
If there is no status
property present on the error, then we fall back to the statusCode
property:
const notFoundError = new Error('Not Found');
notFoundError.statusCode = 404;
const status = getErrorHttpStatus(error); // 404
If the status
or statusCode
property is a string, it will be parsed as an integer before continuing.
In any of the following scenarios, the function will return a default status code of 500
:
400
or greater than 599
The contributing guide is available here. All contributors must follow this library's code of conduct.
Licensed under the MIT license. Copyright © 2024, Rowan Manning