Skip to content

Commit 6960ca8

Browse files
Add null checks when checking for object types (#176)
1 parent dae4524 commit 6960ca8

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/trace/context.spec.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -582,6 +582,15 @@ describe("extractTraceContext", () => {
582582
source: Source.Event,
583583
});
584584
});
585+
it("returns an empty context when headers are null", () => {
586+
const result = extractTraceContext(
587+
{
588+
headers: null,
589+
},
590+
{} as Context,
591+
);
592+
expect(result).toEqual(undefined);
593+
});
585594
it("returns trace read from event with the extractor as the highest priority", () => {
586595
process.env["_X_AMZN_TRACE_ID"] = "Root=1-5ce31dc2-2c779014b90ce44db5e03875;Parent=0b11cc4230d3e09e;Sampled=1";
587596

src/trace/context.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -287,11 +287,11 @@ export function readTraceFromHTTPEvent(event: any): TraceContext | undefined {
287287
}
288288

289289
export function readTraceFromEvent(event: any): TraceContext | undefined {
290-
if (typeof event !== "object") {
290+
if (!event || typeof event !== "object") {
291291
return;
292292
}
293293

294-
if (typeof event.headers === "object") {
294+
if (event.headers !== null && typeof event.headers === "object") {
295295
return readTraceFromHTTPEvent(event);
296296
}
297297

src/utils/request.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export interface HTTPError {
1414
statusCode?: number;
1515
}
1616
export function isHTTPError(error: any): error is HTTPError {
17-
return typeof error === "object" && Object.values(HTTPErrorType).includes(error.type);
17+
return typeof error === "object" && error !== null && Object.values(HTTPErrorType).includes(error.type);
1818
}
1919

2020
export function post<T>(url: URL, body: T, options?: Partial<RequestOptions>): Promise<void> {

0 commit comments

Comments
 (0)