Houston, we have a problem
Howhap is a small library for dealing with parameterized errors.
npm install --save howhap
let Howhap = ; // Each error needs a message and a status code.let authError = message: 'Please enter a password.' status: 400; // Errors can also use simple templatinglet emailError = message: '"{{ email }}" is not a valid email.' status: 400 email: 'fake' ; // You can display error messages as a stringemailError; // "fake" is not a valid email // Or JSONemailError;/* * { * message: '"{{ email }}" is not a valid email.', * status: 400, * params: { email: 'fake' } * } */
Properties
message
type: string
errmessage = 'Something went wrong {{ e }}';
status
type: integer or string representation of an integer
errstatus = 400; errstatus = '500';
params
type: object
errparams = e: 'foo' ;
Methods
Howhap.prototype.toString()
returns: a string representation of the error with parameters replaced.
let err = message: '{{ x }} went wrong' status: 500 x: 'a thing' ;console; // 'a thing went wrong'
Howhap.prototype.toJSON()
returns: an object representation of the error.
let err = message: '{{ x }} went wrong' status: 500 x: 'a thing' ;console;// {// message: '{{ x }} went wrong',// status: 500,// params: { x: 'a thing' }// }
Howhap.prototype.set(obj)
obj: an object with message, status and params properties.
sets all error properties (message, status, params) in one function call.
let err = message: '{{ x }} went wrong' status: 500 x: 'a thing' ;errconsole;// {// message: '{{ model }} was not found',// status: 404,// params: { model: 'User' }// }console; // User was not found