Skip to content

Commit bdec0a0

Browse files
committed
handle strings
1 parent f1f5b06 commit bdec0a0

File tree

2 files changed

+66
-9
lines changed

2 files changed

+66
-9
lines changed

plugins/web/opentelemetry-instrumentation-web-exception/src/instrumentation.ts

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,31 @@ export class WebExceptionInstrumentation extends InstrumentationBase<GlobalError
5151
init() {}
5252

5353
onError = (event: ErrorEvent | PromiseRejectionEvent) => {
54+
const EXCEPTION_EVENT_NAME = 'exception';
5455
const error: Error | undefined =
5556
'reason' in event ? event.reason : event.error;
57+
58+
const eventLogger = events.getEventLogger(
59+
this.instrumentationName,
60+
this.instrumentationVersion
61+
);
62+
63+
if (typeof error === 'string') {
64+
const customAttributes = this.applyCustomAttributes
65+
? this.applyCustomAttributes(error)
66+
: {};
67+
68+
eventLogger.emit({
69+
name: EXCEPTION_EVENT_NAME,
70+
data: {
71+
[ATTR_EXCEPTION_MESSAGE]: error,
72+
},
73+
attributes: customAttributes,
74+
severityNumber: SeverityNumber.ERROR,
75+
timestamp: hrTime(),
76+
});
77+
}
78+
5679
if (error) {
5780
const message = error.message;
5881
const type = error.name;
@@ -62,17 +85,12 @@ export class WebExceptionInstrumentation extends InstrumentationBase<GlobalError
6285
[ATTR_EXCEPTION_STACKTRACE]: error.stack,
6386
};
6487

65-
const eventLogger = events.getEventLogger(
66-
this.instrumentationName,
67-
this.instrumentationVersion
68-
);
69-
7088
const customAttributes = this.applyCustomAttributes
7189
? this.applyCustomAttributes(error)
7290
: {};
7391

7492
eventLogger.emit({
75-
name: 'exception',
93+
name: EXCEPTION_EVENT_NAME,
7694
data: errorAttributes,
7795
attributes: customAttributes,
7896
severityNumber: SeverityNumber.ERROR,

plugins/web/opentelemetry-instrumentation-web-exception/test/instrumentation.test.ts

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,20 @@ describe('WebExceptionInstrumentation', () => {
6666
source?: string,
6767
lineno?: number,
6868
colno?: number,
69-
error?: Error
69+
error?: Error | string
7070
) => {
71-
if (error?.name !== 'ValidationError') {
71+
console.log(error);
72+
if (error instanceof Error && error.name !== 'ValidationError') {
7273
// If we are testing our instrumentation, we want to let the error propagate.
7374
// If it is any other kind of error, we want Mocha to handle the error as expected.
7475
mochaErrorHandler?.call(window, event, source, lineno, colno, error);
7576
}
77+
78+
if (typeof error === 'string' && error !== 'string') {
79+
// If we are testing our instrumentation, we want to let the error propagate.
80+
// If it is any other kind of error, we want Mocha to handle the error as expected.
81+
mochaErrorHandler?.call(window, event, source, lineno, colno);
82+
}
7683
};
7784
});
7885

@@ -140,10 +147,27 @@ describe('WebExceptionInstrumentation', () => {
140147
assert.strictEqual(body[ATTR_EXCEPTION_STACKTRACE], stack);
141148
}, 0);
142149
});
150+
151+
it('should handle throwing an error as a string', async () => {
152+
setTimeout(() => {
153+
throw 'string';
154+
});
155+
156+
setTimeout(() => {
157+
const events = exporter.getFinishedLogRecords();
158+
// assert.ok(events.length > 0, 'Expected at least one log record');
159+
const event = events[0];
160+
const body = event.body as Record<string, any>;
161+
assert.strictEqual(body[ATTR_EXCEPTION_MESSAGE], 'string');
162+
}, 0);
163+
});
143164
});
144165

145166
describe('adding custom attributes', () => {
146-
const applyCustomAttrs = (error: Error) => {
167+
const applyCustomAttrs = (error: Error | string) => {
168+
if (typeof error === 'string') {
169+
return { 'app.custom.exception': error.toLocaleUpperCase() };
170+
}
147171
return {
148172
'app.custom.exception': error.message.toLocaleUpperCase(),
149173
};
@@ -178,5 +202,20 @@ describe('WebExceptionInstrumentation', () => {
178202
);
179203
}, 0);
180204
});
205+
206+
it('should add custom attributes if the error is a string', async () => {
207+
setTimeout(() => {
208+
throw 'string';
209+
});
210+
211+
setTimeout(() => {
212+
const events = exporter.getFinishedLogRecords();
213+
assert.ok(events.length > 0, 'Expected at least one log record');
214+
const event = events[0];
215+
const body = event.body as Record<string, any>;
216+
assert.strictEqual(body[ATTR_EXCEPTION_MESSAGE], 'string');
217+
assert.strictEqual(event.attributes['app.custom.exception'], 'STRING');
218+
}, 0);
219+
});
181220
});
182221
});

0 commit comments

Comments
 (0)