@financial-times/n-error
v1.1.0
Published
a convenient error creator with pure manipulation methods > patterns and tools for error parsing, descriptive error creation, and standardised handling
Downloads
472
Maintainers
Keywords
Readme
n-error
a convenient error creator with pure manipulation methods
patterns and tools for error parsing, descriptive error creation, and standardised handling
quickstart
import nError from '@financial-times/n-error';
throw nError({ status: 404, message: 'sessionId not found', type: 'AUTH_FAILURE' });
throw nError.notFound({ message: 'sessionId not found', type: 'AUTH_FAILURE' });
catch (e) {
throw e.extend({
handler: 'REDIRECT_TO_INDEX',
user: { message: 'Authentification Failed' },
}).remove('message');
}
install
npm install @financial-times/n-error
usage
constructor
import nError from '@financial-times/n-error';
const e = nError({ status: 404 });
console.log(e instanceof nError); // true
console.log(e.stack); // built-in .stack for stack tracing like Error
manipulation
use .extend()
and .remove()
to create new copy of the error that maintains the stack trace if you want the manipulation to be pure to avoid unclear behaviour, e.g. async failure logger attached on a lower level
throw e.extend({ handler: 'SOME_ACTION' }).remove('message');
parse fetch error
parse fetch error into NError object with Category for further error handling
Error or other objects would be thrown as it is
parseFetchError() returns a
Promise
, recommended to useawait
/* api-service */
import { parseFetchError } from '@financial-times/n-error';
try{
await fetch(url, options);
} catch (e) {
throw await parseFetchError(e); // use `await`
}
/* controller/middleware */
import { CATEGORIES } from '@financial-times/n-error';
try {
await APIService.call();
} catch (e) {
// handle the error differently in case of network errors
if(e.catogary === CATEGORIES.FETCH_NETWORK_ERROR){
return next(e.extend({
user: { message: `network error: ${e.code}` }
}));
}
// handle fetch response error in grace
// parsed message according to content-type
// stop `e.json() is not a function` error
if(e.category === CATEGORIES.FETCH_RESPONSE_ERROR){
const { errorCodes } = e.message;
return next(e.extend({
user: { message: errorCodesToUserMessage(errorCodes) }
}));
}
return next(e);
}
patterns
error sources
- fetch response error
- fetch network error
- Error object
- other custom Error object
descriptitive error objects
const e = NError({
status: 404,
message: 'some type of message', // message from server to be logged
handler: 'REDIRECT_TO_INDEX', // describe error handling behaviour
user: { message: 'Authentification Failed' } // override the default message from the server for UI
});
universal error handler
function(e, req, res, next) {
if(e.handler && e.handler === 'REDIRECT_TO_ORIGINAL'){
return res.redirect(303, req.originalUrl);
}
return res.render('errors', message: e.user.message || e.message );
}
reserved fields
operation
, action
, category
, result
(if you use n-auto-logger) are reserved fields that can be overriden, be cautious if you really want to override the default. handler
is recommended to specify the error handler behaviour, which would be filtered by n-auto-logger
.