Files
freeCodeCamp/api-server/server/middlewares/error-reporter.js
Josh Soref 004b99bf8f chore: fix typos in spelling (#38100)
* spelling: accidentally

* spelling: announce

* spelling: assembly

* spelling: avoid

* spelling: backend

* spelling: because

* spelling: claimed

* spelling: candidate

* spelling: certification

* spelling: certified

* spelling: challenge

* spelling: circular

* spelling: it isn't

* spelling: coins

* spelling: combination

* spelling: compliant

* spelling: containers

* spelling: concise

* spelling: deprecated

* spelling: development

* spelling: donor

* spelling: error

* spelling: everything

* spelling: exceed

* spelling: exist

* spelling: falsy

* spelling: faulty

* spelling: forward

* spelling: handle

* spelling: indicates

* spelling: initial

* spelling: integers

* spelling: issealed

* spelling: javascript

* spelling: length

* spelling: maximum

* spelling: minimum

* spelling: mutable

* spelling: notifier

* spelling: coordinate

* spelling: passport

* spelling: perform

* spelling: permuter

* spelling: placeholder

* spelling: progressively

* spelling: semantic

* spelling: submission

* spelling: submit

* spelling: translations

* spelling: turquoise

* spelling: visualization

* spelling: without

* spelling: registration

* spelling: representation
2020-02-08 23:59:10 +05:30

67 lines
1.7 KiB
JavaScript

import debug from 'debug';
import Rollbar from 'rollbar';
import {
isHandledError,
unwrapHandledError
} from '../utils/create-handled-error.js';
import { rollbar } from '../../../config/secrets';
const { appId } = rollbar;
const reporter = new Rollbar(appId);
const log = debug('fcc:middlewares:error-reporter');
const errTemplate = (error, req) => {
const { message, stack } = error;
return `
Time: ${new Date(Date.now()).toISOString()}
Error: ${message}
Is authenticated user: ${!!req.user}
Route: ${JSON.stringify(req.route, null, 2)}
Stack: ${stack}
// raw
${JSON.stringify(error, null, 2)}
`;
};
export function reportError(err) {
return process.env.FREECODECAMP_NODE_ENV === 'production' &&
process.env.ERROR_REPORTER === 'true'
? reporter.error(err.message, err)
: console.error(err);
}
export default function errorReporter() {
if (
process.env.FREECODECAMP_NODE_ENV !== 'production' &&
process.env.ERROR_REPORTER === 'true'
) {
return (err, req, res, next) => {
console.error(errTemplate(err, req));
if (isHandledError(err)) {
// log out user messages in development
const handled = unwrapHandledError(err);
log(handled.message);
}
next(err);
};
}
return (err, req, res, next) => {
// handled errors do not need to be reported,
// they report a message and maybe redirect the user
// errors with status codes shouldn't be reported
// as they are usually user messages
if (isHandledError(err) || err.statusCode || err.status) {
return next(err);
}
// logging the error provides us with more information,
// i.e isAuthenticatedUser, req.route
console.error(errTemplate(err, req));
reportError(err);
return next(err);
};
}