Skip to content
This repository has been archived by the owner on Sep 27, 2023. It is now read-only.
/ LMError Public archive

The general Error class for Node.js projects integrated with HTTP response.

License

Notifications You must be signed in to change notification settings

leismore/LMError

Repository files navigation

This package is obsoleted. Please replace it with @leismore/lmos-nodejs-lmerror


LMError

A general Error class for Node.js projects.

  • Error message & error code
  • HTTP response
  • Error chain

Donation

Buy me a coffee via PayPal Donation

Installation

npm install @leismore/lmerror

Test

npm test

Example

import { LMError, LMErrorErr, LMErrorRes } from '@leismore/lmerror';

let errMessage:LMErrorErr = { message: 'some error', code: 'error_001' };
let httpResponse:LMErrorRes = {
  statusCode: '503',
  headers:    {
    'Retry-After': '10',
    'Content-Type': 'application/json'
  },
  body:       {message: 'Please try again later'}
};
let previousError = new Error('previous error');

throw new LMError(errMessage, httpResponse, previousError);

Class Structure

Properties

public readonly error:       LMErrorErr;
public readonly response?:   LMErrorRes;
public          previous?:   Error;

Methods

public constructor(error: LMErrorErr, response?: LMErrorRes, previous?: Error)
public addPrevious(previous: Error):void
public toString(): string

Error thrown by constructor and addPrevious

Error messages:

  • invalid_error_message
  • invalid_error_code
  • invalid_http_statusCode
  • invalid_http_header
  • invalid_http_body
  • invalid_previous
  • previous_exists

Types

type LMErrorErr = {         // Error
  readonly message: string, // Message for human
  readonly code:    string  // Code for machine
};
type LMErrorRes = {                                     // HTTP response
  readonly statusCode:  string,                         // HTTP response status code
           headers?:   {readonly [key:string]: string}, // HTTP headers
           body?:       any                             // HTTP body
};

Authors

License

GNU Affero General Public License v3.0

Credit

Inspired greatly by http-errors

About

The general Error class for Node.js projects integrated with HTTP response.

Resources

License

Stars

Watchers

Forks

Packages

No packages published