1.0.5 • Published 2 years ago

@switchcase/node-lerror v1.0.5

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

@switchcase/node-lerror

This NPM module is designed to help simplify the management of common HTTP error codes.

Table of Contents

Prerequisites

Installation

yarn add @switchcase/node-lerror

Building

yarn build

Testing

yarn install
yarn test

Usage

The intent of this module is to translate internal javascript API into consistent HTTP Error responses.

// https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
export const handler = async (event: object = {}): Promise<object> => {
  console.log("Received event:", JSON.stringify(event, null, 2));
  const action = event.httpMethod;

  let body = event.body;
  if (event.isBase64Encoded === true) {
    const buff = new Buffer(event.body, "base64");
    body = buff.toString("utf8");
  }

  if (action === "POST") {
    console.log("handling POST calling processEvent");
    try {
      return await processEvent(body);
    } catch (e) {
      console.log("caught exception!", e);
      throw new BadRequestError("Something went wrong");
    }
  } else {
    console.log("throwing BadRequestError");
    throw new BadRequestError("Invalid action");
  }
};

Error Contract

The following are the public properties availabe for these error classes.

Property nameTypeMeaning
namestringProgrammatically-usable name of the error.
messagestringHuman-readable summary of the failure. Programmatically-accessible details are provided through VError.info(err) class method.
stackstringHuman-readable stack trace where the Error was constructed.
statusCodenumberHTTP Status Code that should be presented for this error
{
  name: 'NotFoundError',
  statusCode: 404 ,
  message: "File Not Found",
  stack: Error().stack
}

Background

There are few different standards and conventions by Amazon API Gateway, Amazon Lambda, Node, Express, etc. that may conflict on how to handle errors and tracing such requests. The goal here is to create a single set of Error classes that incorporate the best practices and manages some of the details that will help us more consistently integrate with our observability and REST API contracts.

  1. Be clear about what your function does.
  2. Use Error objects (or subclasses) for all errors, and implement the Error contract.
  3. Use the Error's name property to distinguish errors programmatically.
  4. Augment the Error object with properties that explain details
  5. If you pass a lower-level error to your caller, consider wrapping it instead.

It recommend to standardize on the following property name, so they can be used further down the stack.

Property nameIntended use
localHostnamethe local DNS hostname (e.g., that you're accepting connections at)
localIpthe local IP address (e.g., that you're accepting connections at)
localPortthe local TCP port (e.g., that you're accepting connections at)
remoteHostnamethe DNS hostname of some other service (e.g., that you tried to connect to)
remoteIpthe IP address of some other service (e.g., that you tried to connect to)
remotePortthe port of some other service (e.g., that you tried to connect to)
paththe name of a file, directory, or Unix Domain Socket (e.g., that you tried to open)
srcpaththe name of a path used as a source (e.g., for a rename or copy)
dstpaththe name of a path used as a destination (e.g., for a rename or copy)
hostnamea DNS hostname (e.g., that you tried to resolve)
ipan IP address (e.g., that you tried to reverse-resolve)
propertyNamean object property name, or an argument name (e.g., for a validation error)
propertyValuean object property value (e.g., for a validation error)
syscallthe name of a system call that failed
errnothe symbolic value of errno (e.g., "ENOENT"). Do not use this for errors that don't actually set the C value of errno. Use "name" to distinguish between types of errors.

This package builds on top of the verror NPM package.

Reference