kolomiichenko/graylog-api

Remove callback from try catch

Closed this issue · 0 comments

On index.js there is a callback that is done inside a try catch:

try {
    callback(null, JSON.parse(body));
} catch (err) {
    callback(["Bad response", err, reqUri]);
}

This is a bad practice and should be changed. Imagine that the code inside that callback has some bug and raises an exception. That exception will be caught on that catch block and callback is going to be called again, causing an error of the type Callback was already called. This is also effectively hiding the underneath error, making it hard to find out what the issue is and fix it.

I suggest it should be changed to something like:

var parsedBody;
try {
    parsedBody = JSON.parse(body);
} catch (err) {
    callback(["Bad response", err, reqUri]);
}

callback(null, parsedBody);