Skip to content

Commit

Permalink
refactor: make CollectorExporterError a subclass of Error
Browse files Browse the repository at this point in the history
  • Loading branch information
mwear committed Sep 16, 2020
1 parent e2e66f6 commit 2673088
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 28 deletions.
4 changes: 1 addition & 3 deletions packages/opentelemetry-exporter-collector-proto/src/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,6 @@ export function send<ExportItem, ServiceRequest>(
);
}
} else {
onError({
message: 'No proto',
});
onError(new collectorTypes.CollectorExporterError('No proto'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ export function sendWithHttp<ExportItem, ServiceRequest>(
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);
}
Expand Down
13 changes: 9 additions & 4 deletions packages/opentelemetry-exporter-collector/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
Expand Down

0 comments on commit 2673088

Please sign in to comment.