-
Notifications
You must be signed in to change notification settings - Fork 251
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1307 from bugsnag/plugin-aws-lambda
Add new AWS Lambda plugin
- Loading branch information
Showing
17 changed files
with
732 additions
and
5 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) Bugsnag, https://www.bugsnag.com/ | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the Software | ||
is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# @bugsnag/plugin-aws-lambda | ||
|
||
A [@bugsnag/js](https://github.com/bugsnag/bugsnag-js) plugin for capturing errors in AWS Lambda functions. | ||
|
||
## License | ||
|
||
This package is free software released under the MIT License. See [LICENSE.txt](./LICENSE.txt) for details. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@bugsnag/plugin-aws-lambda", | ||
"version": "7.7.0", | ||
"main": "dist/bugsnag-aws-lambda.js", | ||
"types": "types/bugsnag-plugin-aws-lambda.d.ts", | ||
"description": "AWS Lambda support for @bugsnag/node", | ||
"homepage": "https://www.bugsnag.com/", | ||
"repository": { | ||
"type": "git", | ||
"url": "[email protected]:bugsnag/bugsnag-js.git" | ||
}, | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"files": [ | ||
"dist", | ||
"types" | ||
], | ||
"scripts": { | ||
"clean": "rm -fr dist && mkdir dist", | ||
"build": "npm run clean && ../../bin/bundle src/index.js --node --standalone=BugsnagPluginAwsLambda | ../../bin/extract-source-map dist/bugsnag-aws-lambda.js", | ||
"postversion": "npm run build" | ||
}, | ||
"author": "Bugsnag", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@bugsnag/in-flight": "^7.7.0" | ||
}, | ||
"devDependencies": { | ||
"@bugsnag/core": "^7.7.0" | ||
}, | ||
"peerDependencies": { | ||
"@bugsnag/core": "^7.0.0" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
const bugsnagInFlight = require('@bugsnag/in-flight') | ||
|
||
const BugsnagPluginAwsLambda = { | ||
name: 'awsLambda', | ||
|
||
load (client) { | ||
bugsnagInFlight.trackInFlight(client) | ||
|
||
return { | ||
createHandler ({ flushTimeoutMs = 2000 } = {}) { | ||
return wrapHandler.bind(null, client, flushTimeoutMs) | ||
} | ||
} | ||
} | ||
} | ||
|
||
function wrapHandler (client, flushTimeoutMs, handler) { | ||
let _handler = handler | ||
|
||
if (handler.length > 2) { | ||
// This is a handler expecting a 'callback' argument, so we convert | ||
// it to return a Promise so '_handler' always has the same API | ||
_handler = promisifyHandler(handler) | ||
} | ||
|
||
return async function (event, context) { | ||
client.addMetadata('AWS Lambda context', context) | ||
|
||
try { | ||
return await _handler(event, context) | ||
} catch (err) { | ||
if (client._config.autoDetectErrors && client._config.enabledErrorTypes.unhandledExceptions) { | ||
const handledState = { | ||
severity: 'error', | ||
unhandled: true, | ||
severityReason: { type: 'unhandledException' } | ||
} | ||
|
||
const event = client.Event.create(err, true, handledState, 'aws lambda plugin', 1) | ||
|
||
client._notify(event) | ||
} | ||
|
||
throw err | ||
} finally { | ||
try { | ||
await bugsnagInFlight.flush(flushTimeoutMs) | ||
} catch (err) { | ||
client._logger.error(`Delivery may be unsuccessful: ${err.message}`) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// Convert a handler that uses callbacks to an async handler | ||
function promisifyHandler (handler) { | ||
return function (event, context) { | ||
return new Promise(function (resolve, reject) { | ||
const result = handler(event, context, function (err, response) { | ||
if (err) { | ||
reject(err) | ||
return | ||
} | ||
|
||
resolve(response) | ||
}) | ||
|
||
// Handle an edge case where the passed handler has the callback parameter | ||
// but actually returns a promise. In this case we need to resolve/reject | ||
// based on the returned promise instead of in the callback | ||
if (isPromise(result)) { | ||
result.then(resolve).catch(reject) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
function isPromise (value) { | ||
return (typeof value === 'object' || typeof value === 'function') && | ||
typeof value.then === 'function' && | ||
typeof value.catch === 'function' | ||
} | ||
|
||
module.exports = BugsnagPluginAwsLambda |
Oops, something went wrong.