aboutsummaryrefslogtreecommitdiffstats
path: root/packages/kovan-faucets/src/ts/error_reporter.ts
blob: db21647c04a8f9dfec7bcb61bbad1b4b82ecbb5e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as express from 'express';
import * as fs from 'fs';
import rollbar = require('rollbar');

import {configs} from './configs';
import {utils} from './utils';

export const errorReporter = {
    setup() {
        rollbar.init(configs.ROLLBAR_ACCESS_KEY, {
            environment: configs.ENVIRONMENT,
        });

        rollbar.handleUncaughtExceptions(configs.ROLLBAR_ACCESS_KEY);

        process.on('unhandledRejection', (err: Error) => {
            utils.consoleLog(`Uncaught exception ${err}. Stack: ${err.stack}`);
            this.report(err);
            process.exit(1);
        });
    },
    async reportAsync(err: Error, req?: express.Request): Promise<any> {
        if (configs.ENVIRONMENT === 'development') {
            return; // Do not log development environment errors
        }

        return new Promise((resolve, reject) => {
            rollbar.handleError(err, req, (rollbarErr: Error) => {
                if (rollbarErr) {
                    utils.consoleLog(`Error reporting to rollbar, ignoring: ${rollbarErr}`);
                    reject(rollbarErr);
                } else {
                    resolve();
                }
            });
        });
    },
    errorHandler() {
        return rollbar.errorHandler(configs.ROLLBAR_ACCESS_KEY);
    },
};