Created
August 30, 2019 14:26
-
-
Save pstaender/7400a1815cb88deafd69373832ceb557 to your computer and use it in GitHub Desktop.
Sentry error handling in aws lambda (nodejs)
This file contains hidden or 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
const path = require("path"); | |
const Raven = require("raven"); | |
const lambdaHandler = async event => { | |
throw new Error("test"); | |
return { | |
statusCode: 200, | |
body: JSON.stringify({ message: "ok" }) | |
}; | |
}; | |
const getSourceMaps = data => { | |
const stacktrace = data.exception && data.exception[0].stacktrace; | |
if (stacktrace && stacktrace.frames) { | |
stacktrace.frames.forEach(frame => { | |
frame.filename = "app:///" + path.relative("/var/task/", frame.filename); | |
}); | |
} | |
return data; | |
}; | |
Raven.config(process.env.SENTRY_DSN, { | |
dataCallback: getSourceMaps | |
}).install(); | |
exports.handler = async event => { | |
try { | |
return await lambdaHandler(event); | |
} catch (error) { | |
return new Promise(resolve => { | |
Raven.captureException(error, () => { | |
resolve({ | |
body: JSON.stringify({ | |
err: error.message, | |
msg: "An error has occured. Our developers have been informed." | |
}), | |
statusCode: 500 | |
}); | |
}); | |
}); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment