Skip to content

Instantly share code, notes, and snippets.

@SarasArya
Last active May 15, 2021 22:05
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save SarasArya/8626a311186a39572421b3eb6a9434ba to your computer and use it in GitHub Desktop.
Save SarasArya/8626a311186a39572421b3eb6a9434ba to your computer and use it in GitHub Desktop.
Serverless Sentry Error Handler example
// Node version 10.x AWS Lambda Serverless framework
const Sentry = require('@sentry/node'); // sentry version "@sentry/node": "^5.5.0",
Sentry.init({
dsn: 'https://randomstuff@sentry.io/1234567',
async beforeSend(event) {
console.log('\n Caught an exception \n');
return event;
},
});
module.exports.v1createBill = async (event, context) => {
try {
console.log(sss); // this will throw a Reference Error
}catch(err){
Sentry.configureScope(scope => scope.setExtra('whatever', event.context.body));
Sentry.captureException(err);
await Sentry.flush(2500);
console.log('Submitted the error');
return {
statusCode: err.statusCode || 500,
headers: {
'Access-Control-Allow-Origin': '*', // Required for CORS support to work
},
body: JSON.stringify({ message: err.message }),
};
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment