Skip to content

Instantly share code, notes, and snippets.

@AmazingTurtle
Created March 26, 2018 07:08
Show Gist options
  • Save AmazingTurtle/f15c0aa33aba7a222b96903ff310f590 to your computer and use it in GitHub Desktop.
Save AmazingTurtle/f15c0aa33aba7a222b96903ff310f590 to your computer and use it in GitHub Desktop.
import {AuthenticationError} from '../errors/authenticationError';
export default (app) => {
app.use((error, request, response, next) => {
if (error || response.sentry) { // using Sentry in my specific case aswell
if (error instanceof AuthenticationError) {
response.statusCode = 401;
response.end(
JSON.stringify({
error: 'unauthorized',
eventId: undefined
})
);
} else {
response.statusCode = 500;
if (process.env.NODE_ENV === 'production') {
response.end(
JSON.stringify({
error: undefined,
eventId: response.sentry
})
);
} else {
response.end(
JSON.stringify({
error: error.toString(),
eventId: undefined
})
);
}
}
if (process.env.NODE_ENV !== 'production') {
console.error(error);
}
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment