DimaCrafter / dc-api-core

Simple API core for your projects
MIT License
11 stars 5 forks source link

Global error handling #15

Closed car1ot closed 4 years ago

car1ot commented 4 years ago

I need to collect 500 errors data. What is the best way to access the error handler globally so as not to make try catch blocks in every request on the controller? P.S. in each request error callback, I need to execute the Sentry.captureException(err) for logging errors by a third-party service.

DimaCrafter commented 4 years ago

Added in dc-api-core v0.2.4-1.

startup.js:

const { onError } = require('dc-api-core');
onError(info => console.log(info));

controllers/Test.js

module.exports = class Test {
    error () { throw new Error('Some unexpected error!'); }
}

Console output:

{
    isSystem: false,
    controller: 'Test',
    action: 'error',
    message: 'Some unexpected error!',
    error: <Error object>
}