Skip to content

remscodes/thror

Repository files navigation

Thror

Simple error creation utility

github ci codecov coverage npm version bundle size license

Installation

npm install thror

Usage

Create Error

createError(name, message, extra?)

Example :

import { createError } from 'thror';

const err = createError('MyException', 'Cannot create user without username.', { status: 400 });

console.log(err.name); // MyException
console.log(err.message); // Cannot create user without username.
console.log(err.status); // 400 
console.log(err instanceof Error) // true

throw err; // [MyException: Cannot create user without username.] { status: 400 }

Emit Error

emitError(name, message, extra?)

Example :

import { emitError } from 'thror';

// will immediately throw the created error 
emitError('MyException', 'Cannot create user without username.', { status: 400 }) // [MyException: Cannot create user without username.] { status: 400 }

Extra

interface ErrorExtra {
  // Preserves the error stack.
  // default : false
  withStack?: boolean;

  // The original error.
  // For example in the case you want to display a clearer error with Thror and store the original one.
  original?: any;

  // Whatever you want.
  [key: string]: any;
}

License

MIT © Rémy Abitbol.