Skip to content

Commit 30bffa6

Browse files
committed
fix callback handling
1 parent c70d4a9 commit 30bffa6

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

src/index.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,33 @@ export function wrap(handler: Handler) {
6767
const ctx = trace.setSpan(context.active(), span);
6868

6969
try {
70-
if (callback && handler.constructor.name !== "AsyncFunction" && handler.length === 3) {
71-
console.log("promisify handler");
72-
handler = promisify(handler);
73-
}
74-
const result = await context.with(ctx, handler as (args: any[]) => any, null, event, lambda_context);
75-
span.setAttributes(flatten({ result }) as Attributes);
70+
// if (callback && handler.constructor.name !== "AsyncFunction" && handler.length === 3) {
71+
// console.log("promisify handler");
72+
// handler = promisify(handler);
73+
// }
74+
const result = await context.with(ctx, async (e, lc, cb) => {
75+
const unkownResult = handler(e, lc, (err, res) => {
76+
if (err) {
77+
let error = typeof err === 'string' ? new Error(err) : err;
78+
span.recordException(err);
79+
span.setAttributes(flatten({ error: { name: error.name, message: error.message, stack: error.stack } }) as Attributes);
80+
}
81+
82+
if (res) {
83+
span.setAttributes(({ result: res }) as Attributes);
84+
}
85+
if (cb) {
86+
cb(err, res)
87+
}
88+
span.end();
89+
});
90+
91+
if (unkownResult) {
92+
return await unkownResult
93+
}
94+
}, null, event, lambda_context, callback);
95+
96+
span.setAttributes(({ result }) as Attributes);
7697
span.end();
7798
return result;
7899
} catch (e) {

tests/wrap.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const context = {
5555
}
5656
describe("wrap", () => {
5757
const { getSpan } = setupOtelTestHarness()
58-
test.only("should wrap a callback lambda handler and not error", async () => {
58+
test("should wrap a callback lambda handler and not error", async () => {
5959
const wrapped = wrap(callbackHandler);
6060
await wrapped({}, context, (err, result) => {
6161
expect(result).toBe("callback lambda go brrr");

0 commit comments

Comments
 (0)