Skip to content

Commit

Permalink
fix: handle non-error objects thrown gracefully (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
vladfrangu committed Nov 2, 2022
1 parent f7ad886 commit c3a4e1a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/utils/src/internals/error_tracker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@ const mergeMessages = (a: string, b: string, storage: Record<string, unknown>) =
const getErrorMessageGroup = (error: ErrnoException, storage: Record<string, unknown>, showFullMessage: boolean) => {
let { message } = error;

if (!message) {
message = typeof error === 'string' ? error : `Unknown error message. Received non-error object: ${JSON.stringify(error)}`;
}

if (!showFullMessage) {
const newLineIndex = message.indexOf('\n');
message = message.slice(0, newLineIndex === -1 ? undefined : newLineIndex);
Expand Down
9 changes: 9 additions & 0 deletions packages/utils/test/non-error-objects-working.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { ErrorTracker } from '../src/internals/error_tracker';

describe('ErrorTracker', () => {
test('processing a non-error error should not crash', () => {
const errorTracker = new ErrorTracker();
// @ts-expect-error tracking non-error errors
expect(() => errorTracker.add('foo')).not.toThrow();
});
});

0 comments on commit c3a4e1a

Please sign in to comment.