Skip to content

Commit

Permalink
Merge pull request #1307 from bugsnag/plugin-aws-lambda
Browse files Browse the repository at this point in the history
Add new AWS Lambda plugin
  • Loading branch information
imjoehaines authored Mar 2, 2021
2 parents daa4936 + 49fad3d commit ff219fb
Show file tree
Hide file tree
Showing 17 changed files with 732 additions and 5 deletions.
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ module.exports = {
project('node plugins', [
'delivery-node',
'in-flight',
'plugin-aws-lambda',
'plugin-express',
'plugin-koa',
'plugin-restify',
Expand Down
19 changes: 19 additions & 0 deletions packages/plugin-aws-lambda/LICENSE.txt
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.
7 changes: 7 additions & 0 deletions packages/plugin-aws-lambda/README.md
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.
35 changes: 35 additions & 0 deletions packages/plugin-aws-lambda/package.json
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"
}
}
84 changes: 84 additions & 0 deletions packages/plugin-aws-lambda/src/index.js
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
Loading

0 comments on commit ff219fb

Please sign in to comment.