Go to rebrand

Errors and Tracking Modules

This section of the styleguide talks about how we handle the flow of errors and tracking. At the moment of making this guide we are using Sentry as our main tracking tool.

In this page we will focus on how to use the error and tracking modules to show and identify issues in sentry efficiently

How to use

1. You will need to import the error as modules in the scripts you want to throw or log, i.e:

import CAError from '@consumeraffairs/ca-styleguide/modules/ca_styleguide_logger/ca-error.es6'

2. Then you can simply throw a normal error or just create an error object and use it and it will automatically report the error to sentry.

I will create a simple example below and I will explain, for this example I will use JSON.parse as it is one of the most common cases of error catching in javascript:

import CAError from '@consumeraffairs/ca-styleguide/modules/ca_styleguide_logger/ca-error.es6' function myFunction(volatileObject) { try { const parsedJson = JSON.parse(volatileObject); } catch (error) { throw new CAError('Error processing json at myFunction', volatileObject); } }

Efficient error tracking

By using custom errors such as CAError you are unblocking some especial features that will help you and your team to track, debug and identify issues more efficiently, this is how:

Unique error types

If you noticed the example above you will immediatly catch that CAError is a custom error constructor, this means that when reported the error type is easy to identify in any tracking tool, here an example on how it will appear on sentry:

ca error example

Unique sentry tags

Sentry allows us to group and identify errors by tags, we are now taking advantage of it by registering our error types constructor names and sending it to classify our errors, this means that if you use CAError it will send CAError as the error.type tag, you can see this in the example below:

error type example

You can click any of the elements shown in the image above and that will take you to a page that will classify the errors by CAError, you can see the example below:

error type example

Context matters

With this new API you are now able to send meaningful personalized data to debug errors and identify flaws more efficiently in sentry, we can send any type of data but for consistency we have decided to send objects as additional context data

Adding context data to errors is as easy as cake, you just have to pass a second object parameter to the custom error constructor:

import CAError from '@consumeraffairs/ca-styleguide/modules/ca_styleguide_logger/ca-error.es6' const contextObject = { key: 'value' }; throw new CAError('Error processing json at myFunction', contextObject); By doing this we are now able to track critical information about scripts failures and errors, some good examples of information to send are:
  • Current state at the moment of failure
  • Selected script information that could help debug the errors
  • Module inputs, data that was passed that made the module fail

Here an example of how the context data would show on Sentry:

Non-blocking error reporting

There could be a case in which you want to log an error to sentry but you don't want to stop script execution, there are two ways to accomplish this:

Use a custom error constructor without throwing

new CAError('Error processing json at myFunction', contextObject); This logs information to sentry in the same way as throwing does, the only difference is that it does not stop the execution. You can also save the error in a variable and throw later at the end of the script if desired.

The Logger API - Sending different types of data (error reporting is not everything)

Not everything is an error and that is why the logger API exists, it is basically the core (A wrapper) in charge of sending and controlling the flow of reporting to Sentry, this is the API used by our custom errors and it allows you to send multiple level of information like:

1. info
2. warning
3. error
4. fatal
5. debug

How to use

import Logger from '@consumeraffairs/ca-styleguide/modules/ca_styleguide_logger/logger.es6'; const logger = new Logger(); logger.captureMessage( 'custom message', 'info' // level of report - {string} one of (info, warning, error, fatal, debug), { key: value } // additional context {Object} );

real case example

import Logger from '@consumeraffairs/ca-styleguide/modules/ca_styleguide_logger/logger.es6'; function myFunction(volatileObject) { try { const parsedJson = JSON.parse(volatileObject); } catch (error) { const logger = new Logger(); logger.captureMessage( 'Problem while processing json at myFunction', 'warning', volatileObject ); } } This will log information to sentry depending on the level selected like: