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
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import {
isEndOfLifeVersion,
} from '../utils/end-of-life-server';
import type { ImportConnectionOptions } from '@mongodb-js/connection-storage/provider';
import { getErrorCodeCauseChain } from '../utils/telemetry';

export type ConnectionsEventMap = {
connected: (
Expand Down Expand Up @@ -1278,6 +1279,7 @@ const connectionAttemptError = (
async () => {
const trackParams = {
error_code: err.code,
error_code_cause_chain: getErrorCodeCauseChain(err),
error_name: err.codeName ?? err.name,
};
if (connectionInfo) {
Expand Down
44 changes: 44 additions & 0 deletions packages/compass-connections/src/utils/telemetry.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
trackConnectionDisconnectedEvent,
trackConnectionCreatedEvent,
trackConnectionRemovedEvent,
getErrorCodeCauseChain,
} from './telemetry';
import { expect } from 'chai';
import type { ConnectionInfo } from '@mongodb-js/connection-storage/renderer';
Expand Down Expand Up @@ -64,4 +65,47 @@ describe('Connections telemetry', function () {
expect(event).to.equal('Connection Removed');
expect(properties).to.deep.equal(expected);
});

describe('#getErrorCodeCauseChain', function () {
it('should return undefined when no error', function () {
const result = getErrorCodeCauseChain(undefined);
expect(result).to.be.undefined;
});

it('should return undefined when there are no error codes', function () {
const result = getErrorCodeCauseChain({});
expect(result).to.be.undefined;
});

it('should return an array with the error code', function () {
const error: any = new Error('Test error');
error.code = 123;

const result = getErrorCodeCauseChain(error);
expect(result).to.deep.equal([123]);
});

it('should return an array of error codes from the cause chain', function () {
const error: Error & { code?: number } = new Error('Test error');
error.code = 123;

// No code / codeName on error two.
const errorTwo = new Error('Test error two');

const errorThree: Error & { codeName?: string } = new Error(
'Test error three'
);
errorThree.codeName = 'PINEAPPLE';

const errorFour: Error & { code?: number } = new Error('Test error four');
errorFour.code = 1111;

error.cause = errorTwo;
errorTwo.cause = errorThree;
errorThree.cause = errorFour;

const result = getErrorCodeCauseChain(error);
expect(result).to.deep.equal([123, 'PINEAPPLE', 1111]);
});
});
});
22 changes: 22 additions & 0 deletions packages/compass-connections/src/utils/telemetry.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,25 @@ export function trackConnectionRemovedEvent(
): void {
track('Connection Removed', {}, connectionInfo);
}

export function getErrorCodeCauseChain(
err: unknown
): (string | number)[] | undefined {
const errorCodesInCauseChain: (string | number)[] = [];
let current = err;

while (current && typeof current === 'object') {
if ('code' in current && current.code) {
errorCodesInCauseChain.push(current.code as string | number);
} else if ('codeName' in current && current.codeName) {
errorCodesInCauseChain.push(current.codeName as string | number);
}
current = (current as { cause?: unknown }).cause;
}

if (errorCodesInCauseChain.length === 0) {
return undefined;
}

return errorCodesInCauseChain;
}
6 changes: 6 additions & 0 deletions packages/compass-telemetry/src/telemetry-events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,12 @@ type ConnectionFailedEvent = ConnectionScopedEvent<{
* The error name.
*/
error_name: string;

/**
* The error codes (or code names) from the error's cause chain.
* The driver and the OIDC library we use are two places that use cause chains.
*/
error_code_cause_chain: (string | number)[] | undefined;
} & ExtraConnectionData;
}>;

Expand Down
Loading