error-factory-js
A JavaScript Factory for creating and handling errors
This module can be used to create custom JavaScript Errors and handle them.
When creating an Error a function can be passed to it, so when catching it you can handle it by calling error.handle(args);
Also this module knows which Errors are generated by it so you can create generic handlers for Errors that were created by it.
Handling Errors is simple and can be either sync or async (Using promises) by using the ErrorFactory's methods (see Examples)
Documentation
This module supports a simple mode to create errors and an extended mode to access the ErrorFactory and its methods
The simple mode can be accessed by using
const ErrorFactory = ;var MyError = ;throw ;
In this context the callback and extras are optional, though
they can be accessed by using
error.handle()
and error.extras
The extended mode can be accessed by using
const ErrorFactory = ;
and call ErrorFactory().someMethod()
or ErrorFactory.someMethod()
to access the method
The supported methods are:
setPromiseLibrary(promiseLib)
This method sets the promise library to be used for handleAsync
const ErrorFactory = ErrorFactory // use builtin promise library
create(name, msg, callback, extras)
This method is equivalent to the simple mode's error creation
try throw ErrorFactory; catcherr errmessage // The error message err // To call the handle function errextras // The extras
exists(error)
This method returns true if the error was created by this ErrorFactory, false otherwise
try throw ErrorFactory; catcherr ErrorFactory // This is true
canHandle(error)
This method returns true if a callback was specified when creating an error, false otherwise
try var MyError = throw ; catcherr ErrorFactory // This is true
handle(error)
This method handles the error and returns true if the error was handled false otherwise
try var MyError = throw ; catcherr ErrorFactory // This is true and error was handled using callback
handleAsync(error)
This method is like handle() but handles the error using Promises and returns a promise. If the error cannot be handled returns a rejected promise with the error.
try var MyError = throw ; catcherr ErrorFactory // This is true and error will be handled using callback
expressHandler()
This method is a generic express handler that returns a function with a signature (err, req, res, next)
The express handler takes a javascript object as options.
Options {handleAsync: boolean}
if you want to use handle or handleAsync, default false
See examples below in how to use it
var { console // prints true res} app app; app
remove(error)
Removes an error from this factory, so now it can not be handled by handle(), handleAsync() etc.
try var MyError = throw ; catcherr ErrorFactory; ErrorFactory // This is false ErrorFactory // This is false ErrorFactory // This is false ErrorFactory // this is false
flush()
Removes all errors from this factory
try var MyError = throw ; catcherr ErrorFactory; ErrorFactory // This is false ErrorFactory // This is false ErrorFactory // This is false ErrorFactory // this is false
addHandler(name, handler) / getHandler(name)
Sets/gets a handler with a name for later use Following code is for express, but can be used for other instances too.
const app = ;const ErrorFactory = ;app var { // implement something} ErrorFactory; app; app
getErrorConstructor(name)
Gets the constructor of a specified error by name.
This is useful when creating errors through the ErrorFactory create()
method.
try throw ErrorFactory; catcherr console // This is true errmessage // The error message err // To call the handle function errextras // The extras
Tests
You can run tests with npm test.
You must install the dev dependencies though:
mocha
should
Examples
You can find examples under the examples folder.
Below are some examples of error-factory-js usage.
Simple Example
const ErrorFactory = ; try var MyError = ; throw ; catch error console; // Prints This is an error
Simple Example by using ErrorFactory's create method
const ErrorFactory = ; try throw ErrorFactory; catch error console; // Prints This is some err
Simple Example with handle function
const ErrorFactory = ; var { console // This refers to the error // If called from error context} try var MyError = ; throw ; catch error error; // Will print Some other message
Simple Example with handle function by using ErrorFactory's sync handle
const ErrorFactory = ; var { console // This refers to the error} try var MyError = ; throw ; catch error if ErrorFactory console else console; // Handle error manually in case handleFunc was not defined // Or the error was not created by this ErrorFactory // The output of this should be// Some other message too// Error Handled
Simple Example with handle function by using ErrorFactory's sync handle
const ErrorFactory = ; var { console // This refers to the error} try var MyError = ; throw ; catch error if ErrorFactory console else console; // Handle error manually in case handleFunc was not defined // Or the error was not created by this ErrorFactory // The output of this should be// Error Handled// Another other message
Simple Example with Express and using ErrorFactory's handle/handleAsync
const app = ;const ErrorFactory = ; var { resstatus500;} app; app; // This code will send a status 500 with message 'Hey something went wrong'
Simple Example with Express and using ErrorFactory's expressHandler
const app = ;const ErrorFactory = ; var { resstatus500;} app; app;// If the error can be handled by the express handler next() is called after// handling the error else next(err) is called // so be sure to add this before the generic error handler// and not use any res.send below this point app; // This code will send a status 500 with message 'Hey something went wrong'