Skip to content
This repository has been archived by the owner on Aug 23, 2021. It is now read-only.
/ aws-apig-responses Public archive

A Error derived object factory designed to provide an easy map to AWS API Gateway error regex.

License

Notifications You must be signed in to change notification settings

zippadd/aws-apig-responses

Repository files navigation

aws-apig-responses

npm Build Status codecov dependencies Status devDependencies Status Greenkeeper badge Inline docs

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.

Contribution

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.

Supported Runtimes

  • NodeJS 10+

Usage

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.

HttpError

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

Example

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

About

A Error derived object factory designed to provide an easy map to AWS API Gateway error regex.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •