A Error derived object factory designed to provide an easy map to AWS API Gateway error regex. This is accomplished through prepending the status code (e.g. 404) and pascal code id (e.g. NotFound) to the Error message.
Supports client (e.g. 404), server (e.g. 500), and non errors (e.g. 302) for maximum flexibility. Utilizes Nodes built-in HTTP module for status codes and descriptions.
I welcome bug reports, feature requests, and pull requests to improve the module! Can't guarantee they'll necessarily be implemented/incorporated, but will do my best to consider. If you do plan a pull request for a larger feature, please create an issue to discuss first.
- NodeJS 10+
const resp = require("aws-apig-responses");
/* 404 Not Found */
throw new resp.NotFound();
throw new resp.NotFound("These aren't the droids you're looking for");
throw new resp[404]();
throw new resp[404]("These aren't the droids you're looking for");
/* 500 Internal Server Error */
throw new resp.InternalServerError();
throw new resp.InternalServerError("Mistakes were made");
throw new resp[500]();
throw new resp[500]("Mistakes were made");
/* 302 Found */
throw new resp.Found("https://redirect-me-here12345.com");
throw new resp[302]("https://redirect-me-here12345.com");
NOTE: 302 does not return the Location header natively, but is intended to provide a location for the URL that can be referenced in a mapping template
All HTTP codes are available either using the pascal case name (e.g. BadRequest, ServiceUnavailable, etc. [including ImATeapot!]) or numeric code. For a full list, please reference http.STATUS_CODES in the Node HTTP module documentation.
Upon invoking new, a subclass of HttpError, which is a subclass of Error, with the prepended message is returned. Details below.
Base class for the specific HTTP errors Supports non-errors, such as 302 Redirect, but since API Gateway requires an Error, this extends Error and is named accordingly
Kind: global class
Properties
Name | Type | Description |
---|---|---|
message | string |
Message for the response. A status code and pascal case identifier are prepended. e.g. [404][NotFound] |
statusCode | string |
HTTP status code e.g. 404 |
statusId | string |
Pascal case identifier of the HTTP status (e.g. NotFound) |
status | string |
Same as statusCode |
origMessage | string |
The original non-prepended message |
throw new resp.NotFound("These aren't the droids you're looking for");
- Returns: NotFoundError
- Message: [404][NotFound]: These aren't the droids you're looking for