diff --git a/packages/opentelemetry-exporter-collector-proto/src/util.ts b/packages/opentelemetry-exporter-collector-proto/src/util.ts index d43fc8645d7..05e4ec00fc9 100644 --- a/packages/opentelemetry-exporter-collector-proto/src/util.ts +++ b/packages/opentelemetry-exporter-collector-proto/src/util.ts @@ -80,8 +80,6 @@ export function send( ); } } else { - onError({ - message: 'No proto', - }); + onError(new collectorTypes.CollectorExporterError('No proto')); } } diff --git a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts index 4262e5cfdc2..b33afe643c5 100644 --- a/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector-proto/test/CollectorTraceExporter.test.ts @@ -161,9 +161,9 @@ describe('CollectorTraceExporter - node with proto over http', () => { const callback = args[1]; callback(mockResError); setTimeout(() => { - const response: any = spyLoggerError.args[0][0]; + const response = spyLoggerError.args[0][0] as string; - assert.strictEqual(response, 'code: 400'); + assert.ok(response.includes('code: 400')); assert.strictEqual(responseSpy.args[0][0], 1); done(); }); diff --git a/packages/opentelemetry-exporter-collector/src/platform/browser/util.ts b/packages/opentelemetry-exporter-collector/src/platform/browser/util.ts index 853814c4553..b25f5cc9b73 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/browser/util.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/browser/util.ts @@ -34,7 +34,9 @@ export function sendWithBeacon( logger.debug('sendBeacon - can send', body); onSuccess(); } else { - const error = { message: 'sendBeacon - cannot send', body }; + const error = new collectorTypes.CollectorExporterError( + `sendBeacon - cannot send ${body}` + ); globalErrorHandler(error); onError(error); } @@ -71,12 +73,11 @@ export function sendWithXhr( logger.debug('xhr success', body); onSuccess(); } else { - const error = { - body, - xhrData: xhr, - code: xhr.status, - message: xhr.responseText, - }; + const error = new collectorTypes.CollectorExporterError( + xhr.responseText, + xhr.status + ); + globalErrorHandler(error); onError(error); } diff --git a/packages/opentelemetry-exporter-collector/src/platform/node/util.ts b/packages/opentelemetry-exporter-collector/src/platform/node/util.ts index d62ff23dfb4..be224a5480b 100644 --- a/packages/opentelemetry-exporter-collector/src/platform/node/util.ts +++ b/packages/opentelemetry-exporter-collector/src/platform/node/util.ts @@ -55,10 +55,10 @@ export function sendWithHttp( collector.logger.debug(`statusCode: ${res.statusCode}`); onSuccess(); } else { - const error = { - code: res.statusCode, - message: res.statusMessage || '', - }; + const error = new collectorTypes.CollectorExporterError( + res.statusMessage, + res.statusCode + ); globalErrorHandler(error); onError(error); } diff --git a/packages/opentelemetry-exporter-collector/src/types.ts b/packages/opentelemetry-exporter-collector/src/types.ts index d707b672786..bf142930e88 100644 --- a/packages/opentelemetry-exporter-collector/src/types.ts +++ b/packages/opentelemetry-exporter-collector/src/types.ts @@ -269,16 +269,21 @@ export namespace opentelemetryProto { /** * Interface for handling error */ -export interface CollectorExporterError { - code?: number; - message: string; - stack?: string; +export class CollectorExporterError extends Error { + readonly code?: number; + readonly name: string = 'CollectorExporterError'; + + constructor(message?: string, code?: number) { + super(message); + this.code = code; + } } /** * Interface for handling export service errors */ export interface ExportServiceError { + name: string; code: number; details: string; metadata: { [key: string]: unknown }; diff --git a/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts index 99682329e28..ed1436b5eb3 100644 --- a/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/browser/CollectorMetricExporter.test.ts @@ -332,8 +332,6 @@ describe('CollectorMetricExporter - web', () => { request.respond(400); const response = spyLoggerError.args[0][0] as string; - assert.ok(response.includes('body: ')); - assert.ok(response.includes('xhrData: ')); assert.ok(response.includes('code: 400')); assert.strictEqual(spyBeacon.callCount, 0); diff --git a/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts index 892d86e518a..2817d46eb08 100644 --- a/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/browser/CollectorTraceExporter.test.ts @@ -253,8 +253,6 @@ describe('CollectorTraceExporter - web', () => { const response = spyLoggerError.args[0][0] as string; - assert.ok(response.includes('body: ')); - assert.ok(response.includes('xhrData: ')); assert.ok(response.includes('code: 400')); assert.strictEqual(spyBeacon.callCount, 0); diff --git a/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts b/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts index 7547831b667..2700aceb468 100644 --- a/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/node/CollectorMetricExporter.test.ts @@ -220,9 +220,9 @@ describe('CollectorMetricExporter - node with json over http', () => { const callback = args[1]; callback(mockResError); setTimeout(() => { - const response: any = spyLoggerError.args[0][0]; - assert.strictEqual(response, 'code: 400'); + const response = spyLoggerError.args[0][0] as string; + assert.ok(response.includes('code: 400')); assert.strictEqual(responseSpy.args[0][0], 1); done(); }); diff --git a/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts b/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts index 22e0c7eb88c..bc6e36dfafe 100644 --- a/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts +++ b/packages/opentelemetry-exporter-collector/test/node/CollectorTraceExporter.test.ts @@ -175,9 +175,9 @@ describe('CollectorTraceExporter - node with json over http', () => { const callback = args[1]; callback(mockResError); setTimeout(() => { - const response: any = spyLoggerError.args[0][0]; + const response = spyLoggerError.args[0][0] as string; - assert.strictEqual(response, 'code: 400'); + assert.ok(response.includes('code: 400')); assert.strictEqual(responseSpy.args[0][0], 1); done(); });