Skip to content

Commit

Permalink
chore: update aws-lambda-typescript example
Browse files Browse the repository at this point in the history
  • Loading branch information
subzero10 committed Dec 23, 2023
1 parent 3ba6572 commit c95f5dd
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions packages/js/examples/aws-lambda-typescript/app/handler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Handler, Context } from 'aws-lambda';
import type { Handler, Context, APIGatewayProxyEvent } from 'aws-lambda';
import honeybadgerWrapper from './honeybadger';
import dotenv from 'dotenv';
import path from 'path';
Expand All @@ -14,6 +14,32 @@ const formatJSONResponse = (response) => {
}
}

const shouldReport = (event: APIGatewayProxyEvent) => {
if (!event.body) {
return false;
}

let body: string | { report: string } | undefined = event.body;
if (typeof body === 'string') {
body = JSON.parse(body);
}

return (body as { report: string }).report === 'yes';
}

const shouldTimeout = (event: APIGatewayProxyEvent) => {
if (!event.body) {
return false;
}

let body: string | { timeout: string } | undefined = event.body;
if (typeof body === 'string') {
body = JSON.parse(body);
}

return (body as { timeout: string }).timeout === 'yes';
}

export const hello: Handler = honeybadgerWrapper(async (event, _context: Context) => {
const hbKey = !!process.env.HONEYBADGER_API_KEY;
return formatJSONResponse({
Expand All @@ -23,7 +49,7 @@ export const hello: Handler = honeybadgerWrapper(async (event, _context: Context
});

export const syncError: Handler = honeybadgerWrapper(async (event) => {
const willReport = event.body && event.body.report === 'yes';
const willReport = shouldReport(event);
if (willReport) {
throw new Error('sync-error');
}
Expand All @@ -43,7 +69,7 @@ export const asyncError = honeybadgerWrapper(async (event) => {
});
}

const willReport = event.body && event.body.report === 'yes';
const willReport = shouldReport(event);
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.",
Expand All @@ -52,7 +78,7 @@ export const asyncError = honeybadgerWrapper(async (event) => {
});

export const callbackError= honeybadgerWrapper((event, _context, callback) => {
const willReport = event.body && event.body.report === 'yes';
const willReport = shouldReport(event);
if (willReport) {
callback(new Error('callback-error'));
return;
Expand All @@ -66,7 +92,7 @@ export const callbackError= honeybadgerWrapper((event, _context, callback) => {
});

export const setTimeoutError = honeybadgerWrapper((event, _context, callback) => {
const willReport = event.body && event.body.report === 'yes';
const willReport = shouldReport(event);
setTimeout(() => {
if (willReport) {
throw new Error('set-timeout-error');
Expand All @@ -81,16 +107,16 @@ export const setTimeoutError = honeybadgerWrapper((event, _context, callback) =>
});

export const timeoutWarning = honeybadgerWrapper(async (event) => {
const asyncThatResolvesAfterTimeout = async (shouldTimeout) => {
const asyncThatResolvesAfterTimeout = async (willTimeout: boolean) => {
return new Promise<void>((resolve, _reject) => {
setTimeout(() => {
resolve()
}, shouldTimeout ? (1000 * 60 * 20) : 200) // 20 minutes
}, willTimeout ? (1000 * 60 * 20) : 200) // 20 minutes
});
}

const shouldTimeout = event.body && event.body.timeout === 'yes';
await asyncThatResolvesAfterTimeout(shouldTimeout);
const willTimeout = shouldTimeout(event);
await asyncThatResolvesAfterTimeout(willTimeout);
return formatJSONResponse({
message: "You summoned the timeoutWarning handler! Nothing was sent to Honeybadger. POST with { 'body': { 'timeout': 'yes' } } to run the function until it times out.",
event,
Expand Down

0 comments on commit c95f5dd

Please sign in to comment.