Skip to content

Commit

Permalink
feat: cleaning up things
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarkarachi committed May 10, 2022
1 parent c9df69e commit 3612900
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/amplify-cli-core/src/errors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class ExportPathValidationError extends Error {}
export class ExportedStackNotFoundError extends Error {}
export class ExportedStackNotInValidStateError extends Error {}
export class DebugConfigValueNotSetError extends Error {}
export class DiagnoseReportUploadError extends Error {}

export class NotInitializedError extends Error {
public constructor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jest.mock('crypto', () => ({
createCipheriv: jest.fn(),
/* eslint-enable spellcheck/spell-checker*/
}));
jest.mock('node-fetch', () => jest.fn().mockReturnValue({ then: jest.fn() }));
jest.mock('node-fetch', () => jest.fn().mockReturnValue({ status: 200 }));

const mockMeta = {
providers: {
Expand Down
21 changes: 13 additions & 8 deletions packages/amplify-cli/src/commands/diagnose.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
stateManager, pathManager, NotInitializedError, spinner,
stateManager, pathManager, NotInitializedError, spinner, DiagnoseReportUploadError,
} from 'amplify-cli-core';
import archiver from 'archiver';
import * as fs from 'fs-extra';
Expand Down Expand Up @@ -87,8 +87,13 @@ const zipSend = async (context: Context, skipPrompts: boolean, error: Error | un
}
if (canSendReport) {
spinner.start('Sending zip');
await sendReport(context, fileDestination);
spinner.succeed('Done');
try {
await sendReport(context, fileDestination);
spinner.succeed('Done');
} catch (ex) {
context.usageData.emitError(ex);
spinner.fail();
}
}
};

Expand Down Expand Up @@ -159,7 +164,7 @@ const createZip = async (context: Context, error: Error | undefined): Promise<st

const sendReport = async (context: Context, fileDestination): Promise<void> => {
const ids = hashedProjectIdentifiers();
const usageDataPayload: UsageDataPayload = context.usageData.getUsageDataPayload(null, "");
const usageDataPayload: UsageDataPayload = context.usageData.getUsageDataPayload(null, '');

await sendFile(fileDestination, {
...ids,
Expand Down Expand Up @@ -188,17 +193,17 @@ const sendFile = async (
const cipherTextBlob = await encryptBuffer(stream, passKey);
const key = await encryptKey(passKey);
const data = JSON.stringify({ ...metaData, key, encryptedFile: cipherTextBlob });
await fetch(report, {
const response = await fetch(report, {
method: 'POST',
headers: {
'content-type': 'application/json',
'content-length': data.length.toString(),
},
body: data,
}).then(r => {
console.log(r);
// no op
});
if (response.status !== 200) {
throw new DiagnoseReportUploadError();
}
};

const hashedProjectIdentifiers = (): { projectIdentifier: string; projectEnvIdentifier: string } => {
Expand Down

0 comments on commit 3612900

Please sign in to comment.