Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Honeybadger.lambdaHandler() to return async handler #680

Merged
merged 24 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased][latest]
### Added
- Nodejs: Include source snippet in backtraces when available (#624)
- `notifyAsync`: Async version of `notify` that returns a promise (#327)

### Changed
- Call afterNotify handlers with error if notify preconditions fail (#654)
- Call beforeNotify handlers even if preconditions fail (#654)
- `Honeybadger.lambdaHandler`: return async or callback based handler based on input handler (#677)

## [3.2.7] - 2021-11-01
### Fixed
Expand Down
2 changes: 2 additions & 0 deletions examples/aws-lambda/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.serverless
node_modules
6 changes: 6 additions & 0 deletions examples/aws-lambda/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# package directories
node_modules
jspm_packages

# Serverless directories
.serverless
27 changes: 27 additions & 0 deletions examples/aws-lambda/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Honeybadger AWS Lambda - Serverless Example
joshuap marked this conversation as resolved.
Show resolved Hide resolved

This example was built and deployed to AWS Lambda with [Serverless.com](https://serverless.com).
The example deploys 5 lambda functions to showcase different scenarios of reporting errors:
- hello -> returns a hello message
- "sync" error -> throws an error in an async handler
- "async" error -> awaits a promise that rejects
- "callback" error -> passes an error to the callback handler (uses the callback-based handler)
- "set timeout" error -> throws an error inside a setTimeout (uses the callback-based handler)

Follow these steps to setup and run the example:

## Setup

1. Install serverless globally: `npm install -g serverless`
2. Install example dependencies: `npm install`
3. Run `serverless` in root folder to configure your serverless installation.
4. If not prompted to deploy from the previous step, run `serverless deploy`.
5. Run `serverless dashboard` to open the dashboard on a web browser and open `honeybadger-io/stage:dev`. Navigate to parameters and add a new key `HONEYBADGER_API_KEY` with your honeybadger api key.
6. Redeploy with `serverless deploy` for the new parameter to take effect.

## Report an error

To report an error:
1. Run `serverless invoke --function syncError --data '{ "body": { "report": "yes" } }'`.
2. Alternatively, if you want to play around without deploying every change you can use `serverless invoke local --function syncError --data '{ "body": { "report": "yes" } }'`.
6. Check your Honeybadger.js dashboard. The error should show up after a few seconds.
74 changes: 74 additions & 0 deletions examples/aws-lambda/handler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'use strict';

const { honeybadgerWrapper } = require('./honeybadger')

const formatJSONResponse = (response) => {
return {
statusCode: 200,
body: JSON.stringify(response)
}
}

module.exports = {
hello: async (event) => {
const hbKey = !!process.env.HONEYBADGER_API_KEY;
return formatJSONResponse({
message: `Hello, welcome to the exciting Serverless world! HB Key available: ${hbKey ? 'yes' : 'no'}`,
event,
});
},
syncError: honeybadgerWrapper(async (event) => {
const willReport = event.body && event.body.report === 'yes';
if (willReport) {
throw new Error("sync-error");
}

return formatJSONResponse({
message: `You summoned the sync-error handler! Nothing was sent to Honeybadger. POST with { 'body': { 'report': 'yes' } } to report to Honeybadger.`,
event,
});
}),
asyncError: honeybadgerWrapper(async (event) => {
const asyncThatThrows = async (shouldThrow) => {
return new Promise((resolve, reject) => {
setTimeout(() => {
(shouldThrow ? reject(new Error("async-error")): resolve())
}, 300);
});
}

const willReport = event.body && event.body.report === 'yes';
await asyncThatThrows(willReport);
return formatJSONResponse({
message: `You summoned the async-error handler! Nothing was sent to Honeybadger. POST with { 'body': { 'report': 'yes' } } to report to Honeybadger.`,
event,
});
}),
callbackError: honeybadgerWrapper((event, context, callback) => {
const willReport = event.body && event.body.report === 'yes';
if (willReport) {
callback(new Error("callback-error"));
return;
}

const resp = formatJSONResponse({
message: `You summoned the callback-error handler! Nothing was sent to Honeybadger. POST with { 'body': { 'report': 'yes' } } to report to Honeybadger.`,
event,
});
callback(null, resp);
}),
setTimeoutError: honeybadgerWrapper((event, context, callback) => {
const willReport = event.body && event.body.report === 'yes';
setTimeout(() => {
if (willReport) {
throw new Error('set-timeout-error');
}

const resp = formatJSONResponse({
message: `You summoned the set-timeout-error handler! Nothing was sent to Honeybadger. POST with { 'body': { 'report': 'yes' } } to report to Honeybadger.`,
event,
});
callback(null, resp);
}, 300);
})
}
11 changes: 11 additions & 0 deletions examples/aws-lambda/honeybadger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const Honeybadger = require('@honeybadger-io/js')

Honeybadger.configure({
environment: 'aws-lambda',
debug: true,
apiKey: process.env.HONEYBADGER_API_KEY
})

module.exports.honeybadgerWrapper = (handler) => {
return Honeybadger.lambdaHandler(handler);
}
Loading