Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Persist downstream exceptions in usage metrics #11711

Merged
merged 2 commits into from
Jan 19, 2023
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
22 changes: 22 additions & 0 deletions packages/amplify-cli/src/__tests__/usage-data.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import url from 'url';
import nock from 'nock';
import * as uuid from 'uuid';

import { AmplifyError } from 'amplify-cli-core';
import { UsageData } from '../domain/amplify-usageData/UsageData';
import { getUrl } from '../domain/amplify-usageData/getUsageDataUrl';
import { Input } from '../domain/input';
import { ManuallyTimedCodePath } from '../domain/amplify-usageData/UsageDataTypes';
import { UsageDataPayload } from '../domain/amplify-usageData/UsageDataPayload';
import { SerializableError } from '../domain/amplify-usageData/SerializableError';

const baseOriginalUrl = 'https://cli.amplify';
const pathToUrl = '/metrics';
Expand Down Expand Up @@ -185,6 +187,26 @@ describe('test usageData calls', () => {
});
});

describe('test usage data payload generation', () => {
it('when no error', async () => {
expect(UsageData.Instance.getUsageDataPayload(null, '').error).toBeUndefined();
expect(UsageData.Instance.getUsageDataPayload(null, '').downstreamException).toBeUndefined();
});
it('when error without downstream exception', async () => {
const amplifyError = new AmplifyError('NotImplementedError', { message: 'test error message' });
const usageData = UsageData.Instance.getUsageDataPayload(amplifyError, '');
expect(usageData.error).toEqual(new SerializableError(amplifyError));
expect(usageData.downstreamException).toBeUndefined();
});
it('when error with downstream exception', async () => {
const downstreamException = new Error('DownStreamException');
const amplifyError = new AmplifyError('NotImplementedError', { message: 'test error message' }, downstreamException);
const usageData = UsageData.Instance.getUsageDataPayload(amplifyError, '');
expect(usageData.error).toEqual(new SerializableError(amplifyError));
expect(usageData.downstreamException).toEqual(new SerializableError(downstreamException));
});
});

const checkUsageData = async (): Promise<void> => {
const abortResponse = await UsageData.Instance.emitAbort();
expect(abortResponse).toBeUndefined();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isCI } from 'amplify-cli-core';
import { $TSAny, isCI } from 'amplify-cli-core';
import { IFlowReport } from 'amplify-cli-shared-interfaces';
import * as os from 'os';
import { Input } from '../input';
Expand All @@ -19,6 +19,7 @@ export class UsageDataPayload implements IUsageDataPayload {
inputOptions: InputOptions;
timestamp: string;
error!: SerializableError;
downstreamException!: SerializableError;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will there always be downstream exception when we initialize UsageDataPayload ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not a non null assertion operator here but a definitive assignment assertion operator since it's not being initialized in the constructor(see the OkGreeter class here)

It can be null, and there is a unit test for that as well (works exactly similar to Error which can also be null)

payloadVersion: string;
osPlatform: string;
osRelease: string;
Expand Down Expand Up @@ -62,6 +63,9 @@ export class UsageDataPayload implements IUsageDataPayload {
this.flowReport = flowReport;
if (error) {
this.error = new SerializableError(error);
if ('downstreamException' in error && (error as $TSAny).downstreamException) {
this.downstreamException = new SerializableError((error as $TSAny).downstreamException as Error);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ export interface IUsageDataPayload {
inputOptions: InputOptions;
timestamp: string;
error: SerializableError;
downstreamException: SerializableError;
payloadVersion: string;
osPlatform: string;
osRelease: string;
Expand Down