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

fix: Add guard for null stack from tracekit. #442

Merged
merged 2 commits into from
Jan 16, 2025
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ export function getStructuredStackTrace(error: Error | undefined) {
const functions: string[] = [];
const urls: string[] = [];

for (const stackFrame of structuredStack) {
if (!Array.isArray(structuredStack)) {
return {};
}

structuredStack.forEach((stackFrame) => {
lines.push(stackFrame.line);
columns.push(stackFrame.column);
functions.push(stackFrame.func);
urls.push(stackFrame.url);
}
});

return {
'exception.structured_stacktrace.columns': columns,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ import {
GlobalErrorsInstrumentation,
} from '../src/global-errors-autoinstrumentation';
import timers from 'node:timers/promises';
import * as tracekit from 'tracekit';

// eslint-disable-next-line @typescript-eslint/no-unsafe-return
jest.mock('tracekit', () => ({
__esModule: true,
...jest.requireActual('tracekit'),
}));

describe('Global Errors Instrumentation Tests', () => {
const exporter = new InMemorySpanExporter();
Expand Down Expand Up @@ -87,6 +94,17 @@ describe('Global Errors Instrumentation Tests', () => {
expect(getStructuredStackTrace(undefined)).toEqual({});
});

it('should return an empty object if StackTrace.stack is null', () => {
const spy = jest.spyOn(tracekit, 'computeStackTrace');
// @ts-expect-error bad data from 3rd part lib
spy.mockReturnValueOnce({ stack: null });

const err = new Error('This is an error');
err.stack = 'garbo';
expect(getStructuredStackTrace(err)).toEqual({});
spy.mockRestore();
});

it('should return an object with structured stack trace information', () => {
const err = new Error('This is an error');
err.stack =
Expand Down
Loading