npm install thror
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 }
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 }
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;
}
MIT © Rémy Abitbol.