4.2.0 • Published 5 months ago

@rowanmanning/get-error-http-status v4.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@rowanmanning/get-error-http-status

Get the HTTP status code for an error object.

Requirements

This library requires the following to run:

Usage

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');

getErrorHttpStatus

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 number before continuing.

In any of the following scenarios, the function will return a default status code of 500:

  • The error is not an error object or a plain JavaScript object
  • The error status property is less than 400 or greater than 599
  • The error status property is a decimal
  • The error status property is a non-numeric string

isErrorHttpStatus

This library exposes a second method named isErrorHttpStatus. You can use this to determine whether a given value is a valid error HTTP status (integer, 400 to 599):

const { isErrorHttpStatus } = require('@rowanmanning/get-error-http-status');

isErrorHttpStatus(500); // true
isErrorHttpStatus(200); // false
isErrorHttpStatus('abc'); // false

Migration

A new major version of this project is released if breaking changes are introduced. We maintain a migration guide to help users migrate between these versions.

Contributing

The contributing guide is available here. All contributors must follow this library's code of conduct.

License

Licensed under the MIT license. Copyright © 2024, Rowan Manning

4.1.0

8 months ago

4.0.0

8 months ago

4.1.2

7 months ago

4.2.0

5 months ago

4.1.1

8 months ago

3.2.2

1 year ago

3.2.1

1 year ago

3.2.0

1 year ago

3.1.0

2 years ago

3.0.1

2 years ago

3.0.0

2 years ago

2.0.1

3 years ago

2.0.0

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago