Skip to content
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
36 changes: 32 additions & 4 deletions packages/core/src/utils/eventbuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ import type { ParameterizedString } from '../types/parameterize';
import type { SeverityLevel } from '../types/severity';
import type { StackFrame } from '../types/stackframe';
import type { StackParser } from '../types/stacktrace';
import { isError, isErrorEvent, isParameterizedString, isPlainObject } from './is';
import { isError, isErrorEvent, isParameterizedString, isPlainObject, isPrimitive } from './is';
import { addExceptionMechanism, addExceptionTypeValue } from './misc';
import { normalizeToSize } from './normalize';
import { normalizeToSize, stringifyValue } from './normalize';
import { extractExceptionKeysForMessage } from './object';
import { stringify } from './string';

/**
* Extracts stack frames from the error.stack string
Expand All @@ -32,13 +33,40 @@ function hasSentryFetchUrlHost(error: unknown): error is Error & { __sentry_fetc
* @hidden
*/
export function _enhanceErrorWithSentryInfo<T extends Error>(error: T): string {
const message = errorMessageToString(error.message);

// If the error has a __sentry_fetch_url_host__ property (added by fetch instrumentation),
// enhance the error message with the hostname.
if (hasSentryFetchUrlHost(error)) {
return `${error.message} (${error.__sentry_fetch_url_host__})`;
return `${message} (${error.__sentry_fetch_url_host__})`;
}

return message;
}

// `message` is typed as a string, but user code can assign anything to it, and event processors
// call string methods on `exception.value`.
function errorMessageToString(message: unknown): string {
if (typeof message === 'string') {
return message;
}

if (message == null) {
return '';
}

if (isPrimitive(message)) {
return String(message);
}

if (isPlainObject(message) || Array.isArray(message)) {
const json = stringify(message, '');
if (json) {
return json;
}
}

return error.message;
return stringifyValue(undefined, message);
}

/**
Expand Down
41 changes: 41 additions & 0 deletions packages/core/test/lib/utils/eventbuilder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,5 +308,46 @@ describe('eventFromMessage', () => {
const exception = exceptionFromError(stackParser, error);
expect(exception.value).toBe('Failed to fetch (localhost:8080)');
});

it('should enhance a non-string message', () => {
const error = new TypeError('placeholder');
(error as { message: unknown }).message = { code: 42 };
addNonEnumerableProperty(error, '__sentry_fetch_url_host__', 'example.com');

const exception = exceptionFromError(stackParser, error);
expect(exception.value).toBe('{"code":42} (example.com)');
});
});
});

describe('exceptionFromError', () => {
const circular: Record<string, unknown> = { code: 42 };
circular.self = circular;

it.each([
['a number', 42, '42'],
['a bigint', BigInt(42), '42'],
['a boolean', false, 'false'],
['undefined', undefined, ''],
['null', null, ''],
['an object', { code: 42 }, '{"code":42}'],
['an array', [1, 'two'], '[1,"two"]'],
['a circular object', circular, '[object Object]'],
['a function', function namedFn() {}, '[Function: namedFn]'],
['an error', new Error('inner'), '[object Error]'],
['a map', new Map([['code', 42]]), '[object Map]'],
[
'a class instance',
new (class CustomMessage {
code = 42;
})(),
'{"code":42}',
],
])('turns %s into a string', (_, message, expected) => {
const error = new Error('placeholder');
(error as { message: unknown }).message = message;

const exception = exceptionFromError(stackParser, error);
expect(exception.value).toBe(expected);
});
});
Loading