A friendly feline companion that helps you build error heirarchies and report application errors to rollbar.
// Import the library. Uses the environment's `ROLLBAR_KEY` to automatically// setup rollbar reporting.constcat=require('error-cat')// Fire-and-forget error reportingcat.report(newError('Something bad'))// Optionally, pass a callback to execute once the error has been reportedcat.report(newError('No good'),function(){// ...})Error cat was designed to be as easy as possible to use with express. Here is an example of how to do so:
constexpress=require('express')constapp=express()// 1. Require error-catconstcat=require('error-cat')// 2. Log and report errors using the static responder methodapp.use(cat.middleware)Error cat exposes a pass-through promise catch handler that will automatically report and then re-throw errors in a promise chain. Here's how to use it:
Promise.try(something).then(somethingElse).catch(cat.catch).catch(function(err){// You'll still need this since `ErrorCat.catch` re-throws the error...})Error-cat exposes a set of extendable error classes that are specifically suited to work with the main library. Application programmers can use these classes to build their own error hierarchies that allow for higher level error handling and management.
These error classes are very well suited for Promise based applications (for
use with .catch) and try-catch based synchronous applications.
In this section we will show you how to use the provided core error classes and extend them to create your own error zoology.
Each of the error types, detailed below, can be extended for your own application.
Building a robust error zoology is key in correctly implmenting a try-catch based
error handling strategy in your application.
Error-cat makes this easy, here's an example of how you can extend the core base error class to automatically increment an error counter in data-dog (via monitor-dog) during construction:
'use strict'constBaseError=require('error-cat/errors/base-error')constmonitor=require('monitor-dog')/** * Base class for all errors that should be monitored in data-dog. * @param {[type]} message Message for the error. * @param {[type]} data Custom data to report to rollbar. */classMonitoredErrorextendsBaseError{constructor(message,data,reporting){super(message,data,reporting)monitor.increment('errors')}}/** * Monitored Error Class * @module my-application:error */module.exports=MonitoredErrorAt the top of the core error hierarchy is BaseError. This class provides the
following:
- An easy-to-use and extendable error class for application programmers
- Allow for meaningful "extra data"
- A method of defining how an error should be reported
Here's an example that shows everything the base error has to offer:
constBaseError=require('error-cat/errors/base-error')// Use it as a normal error...newBaseError('a message')// Pass data that should be reported in the constructor...newBaseError('message',{myData: 123})// Pass reporting information on how it should be reported...newBaseError('message',{},{level: 'critical',// the reporting levelfingerprint: 'mygroup'// a grouping fingerprint})Used to denote an exceptional case that isn't as serious as an error. By default these types of errors are not reported.
Used to denote exceptions that arise during the handling of RESTful routes. The class is automatically annotated with additional metadata via the boom library.
This error class is specifically setup for use in worker servers. It serves as the root error for the ponos worker server library. It specifically sets information about the queue name and the job being processed when the error occured.
Furthermore it exposes two methods that are used by external worker server libraries for automatically setting this data when task handlers for workers throw this type of error:
- (void)
setQueue(name)- Sets the queue name data - (void)
setJob(job)- Sets the job data
Error class that is designed to be thrown when a worker server task handler encounters a scenario where it cannot possibly proceed with the processing of the given job. Worker server implementations should automatically acknowledge the job (even though it was not completed) when encountering this type of error.
An error class designed to be thrown when a worker server task handler encounters a malformed or invalid job.
By default ErrorCat reports errors to rollbar. In order to support other services
the library exposes an AbstractReporter class that can be extended as needed.
The only method that is "required" to define in a subclass is .report, but the
class has many other methods that can be easily overriden.
A full treatment of building a custom reporter is outside the scope of this document.
We advise that you simply read the lib/abstract-reporter.js class file thoroughly
(it is fairly short) to get an understanding how to do so.
For usage reference here is an example of how to implement a custom reporter:
constAbstractReporter=require('error-cat/abstract-reporter')constErrorCat=require('error-cat/error-cat')constnoop=require('101/noop')// 1) Define the reporter...classConsoleReporterextendsAbstractReporter{report(err,cb){if(!this.shouldReport(err)){return(cb||noop)()}console.error(this.getLevel(err),err.message)}}// 2) Use it with a custom error-cat instanceconstcat=newErrorCat(newConsoleReporter('warn'))If you wish to contribute to error-cat please adhere to the following rules:
- Build and read the jsdoc -
npm run doc - Keep test coverage at 100%
- When building new components, please use the same OOP style as
index.js - For PRs include a good title, and a brief yet informative description of what your PR does.
MIT
