Created
March 26, 2018 07:08
-
-
Save AmazingTurtle/f15c0aa33aba7a222b96903ff310f590 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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