@firstfleet/fferrorhandler

handle errors

Usage no npm install needed!

<script type="module">
  import firstfleetFferrorhandler from 'https://cdn.skypack.dev/@firstfleet/fferrorhandler';
</script>

README

@firstfleet/fferrorhandler

A centralized error handler. Uses a custom error object so we not only get the stack trace but we can also track attributes the tie into papertrails log aggregation. Those attributes we are using are, sender, program, message, severity. We also set a custom attribute called isOperational. This denotes if an error is an operational error or a programmer error.

You can also pass in a slack channel name (defaults to alerts_critical) and a boolean, sendToSlack. If an alert is set to critical, it will auto send to slack. If it is not critical, if will only send to slack if sendToSlack is true. You will also need to set an env variable, process.env.SlackPosterURL.

The error handler also handles all uncaughtException, and unhandledRejection. On uncaughtException if the error is Programmer, the process is restarted after the error is logged.

If you update the js docs you can rebuild them by running npm run build-docs

To publish to npm

  1. Increment the package version
  2. Login by running npm login
  3. Make sure you have been added to the firstfleet org
  4. run npm run send

Error Handler

Exports a custom error type AppError. Can be flagged as a programmer error or an operational error. All errors should be capture, when you want to throw an error, import the module

const { AppError } = require('...pathToErrorModule/errorHandler');

Then throw the custom error. The custom error takes in a sender, method, message, severity and a boolean for isOperational

throw new AppError('Node1', 'callApiMethod', 'api call failed', errorLevels.COMMON, true, 'alerts_critical', true);

There is also a built in error handler. You can import it

const { handleError, errorLevels } = require('@firstfleet/fferrorhandler');

Then call it when you want to handle an error. Error levels can be imported from the errorLevels object.

handleError(sender, method, message, severity, isOperational, slackChannelname = 'alerts_critical', sendtToSlack = true, enableStackTrace = true);
handleError('Node1', 'handleInput', 'totally broke the input box by adding script tags', errorLevels.COMMON, true);

The error handler will transform the error into our custom AppError type, send it to syslog (papertrail), and console log it if the process.ENV !== 'production'

Log And Notify Method

A similar error handling method is also available named logAndNotify(). The key difference is it does not automatically get the error stack (sometimes it's more precise to pass in the error stack to get it one level up). This method also takes fewer parameters and posts to Slack based on if a channel was passed on.

const handleError = require('@firstfleet/fferrorhandler');
try{
    let testing = 1
}
catch(error){
    errorHandler.logAndNotify(process.env.APP_NAME, 'myMethod', error.message, error.stack, 'alerts_warning')
}

Handle a list of errors


const {createError, handleErrorList } = require('@firstfleet/fferrorhandler')
let errorList = array.map(item => {
    if (item.error) {
        return  createError(sender, method, message, severity, isOperational, channelId, sendToSlack)
    }
})

handleErrorList(errorList)

Programmer Error

Programmer errors refer to cases where you have no idea why and sometimes where an error came from – it might be some code that tried to read an undefined value or DB connection pool that leaks memory.

Operational Error

Operational errors refer to situations where you understand what happened and the impact of it – for example, a query to some HTTP service failed due to connection problem.