generic-errors

Generic errors with common http codes

Usage no npm install needed!

<script type="module">
  import genericErrors from 'https://cdn.skypack.dev/generic-errors';
</script>

README

generic-errors

Generic error constructors with common http codes.

Also doesn't pollute the stack with its own depth.

Usage

Install:

npm install generic-errors

Require the errors:

const errors = require('generic-errors');

Use an error:

const notFound = new errors.NotFound('Could not find the thing');

Add additional Properties to the error

const unprocessable = new errors.Unprocessable({ message: 'Cant Process', fields: ['foo', bar] });

Available constructors:

BaseError                       // 500 Use to make custom errors, easier to inherit from than Error
BadRequest                      // 400
Unauthorised                    // 401
Forbidden                       // 403
NotFound                        // 404
MethodNotAllowed                // 405
NotAcceptable                   // 406
ProxyAuthenticationRequired     // 407
RequestTimeout                  // 408
Conflict                        // 409
Gone                            // 410
LengthRequired                  // 411
PreconditionFailed              // 412
PayloadTooLarge                 // 413
URITooLong                      // 414
UnsupportedMediaType            // 415
RangeNotSatisfiable             // 416
ExpectationFailed               // 417
Teapot                          // 418
Unprocessable                   // 422
TooEarly                        // 425
UpgradeRequired                 // 426
PreconditionRequired            // 428
TooManyRequests                 // 429
RequestHeaderFieldsTooLarge     // 431
UnavailableForLegalReasons      // 451
NotImplemented                  // 501
BadGateway                      // 502
ServiceUnavailable              // 503
GatewayTimeout                  // 504
HTTPVersionNotSupported         // 505
VariantAlsoNegotiates           // 506
NotExtended                     // 510
NetworkAuthenticationRequired   // 511

Detection

After serialization Generic Errors can still be detected using the isGenericError function attacted to the BaseError constructor.

    const notFound = new errors.NotFound();
    const serializedError = JSON.parse(JSON.stringify(notFound));

    errors.BaseError.isGenericError(serializedError);  // true