Skip to content

Commit 811e8fc

Browse files
ykethanjhockett
andauthored
feat: display error message instead of object (#12636)
* feat: display error message instead of object * Update packages/amplify-util-mock/src/func/index.ts Co-authored-by: John Hockett <[email protected]> * feat: update printer and fix tests * fix: api-extract * fix: nested statements --------- Co-authored-by: John Hockett <[email protected]>
1 parent 0819db4 commit 811e8fc

File tree

5 files changed

+18
-8
lines changed

5 files changed

+18
-8
lines changed

packages/amplify-prompts/API.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class AmplifyPrinter implements Printer {
1919
// (undocumented)
2020
debug: (line: string) => void;
2121
// (undocumented)
22-
error: (line: string) => void;
22+
error: (line: string, error?: any) => void;
2323
// Warning: (ae-forgotten-export) The symbol "Color" needs to be exported by the entry point index.d.ts
2424
//
2525
// (undocumented)
@@ -180,7 +180,7 @@ export type Printer = {
180180
blankLine: () => void;
181181
success: (line: string) => void;
182182
warn: (line: string) => void;
183-
error: (line: string) => void;
183+
error: (line: string, error?: any) => void;
184184
};
185185

186186
// @public (undocumented)

packages/amplify-prompts/src/__tests__/printer.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,9 @@ it('prints error line when silent flag is set', () => {
8181
printer.error(testInput);
8282
expect(writeStream_stub.write.mock.calls[0][0].trim()).toMatchInlineSnapshot(`"🛑 this is a test line"`);
8383
});
84+
85+
it('prints error message when the error is an object', () => {
86+
printer.error('', new Error(testInput));
87+
88+
expect(writeStream_stub.write.mock.calls[1][0].trim()).toMatchInlineSnapshot(`"this is a test line"`);
89+
});

packages/amplify-prompts/src/printer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,13 @@ export class AmplifyPrinter implements Printer {
3030
this.writeLine(`${isHeadless ? '' : '⚠️ '}${chalk.yellow(line)}`);
3131
};
3232

33-
error = (line: string): void => {
33+
// disable-eslint-next-line @typescript-eslint/no-explicit-any
34+
error = (line: string, error?: any): void => {
3435
this.writeLine(`${isHeadless ? '' : '🛑 '}${chalk.red(line)}`);
36+
const errorMessage = error?.message ?? error;
37+
if (errorMessage) {
38+
this.writeLine(`${chalk.red(errorMessage)}`);
39+
}
3540
};
3641

3742
private writeSilenceableLine = (line?: string): void => {
@@ -59,7 +64,7 @@ export type Printer = {
5964
blankLine: () => void;
6065
success: (line: string) => void;
6166
warn: (line: string) => void;
62-
error: (line: string) => void;
67+
error: (line: string, error?: any) => void;
6368
};
6469

6570
type Color = 'green' | 'blue' | 'yellow' | 'red' | 'reset';

packages/amplify-util-mock/src/__tests__/func/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ describe('function start', () => {
6666
prompter_mock.pick.mockResolvedValue(['funcName']);
6767
await start(context_stub);
6868
expect(printer_mock.error.mock.calls[0][0]).toMatchInlineSnapshot(`"funcName failed with the following error:"`);
69-
expect(printer_mock.info.mock.calls[2][0]).toMatchSnapshot();
69+
expect(printer_mock.error.mock.calls[0][1]).toMatchSnapshot();
7070
context_stub.input.options.timeout = 1;
7171
});
7272

7373
it('times out function execution at the specified time', async () => {
7474
getInvoker_mock.mockResolvedValueOnce(() => new Promise((resolve) => setTimeout(() => resolve('lambda value'), 2000)));
7575
await start(context_stub);
76-
expect(printer_mock.info.mock.calls[2][0]).toMatchSnapshot();
76+
expect(printer_mock.error.mock.calls[0][1]).toMatchSnapshot();
7777
});
7878

7979
it('triggers a dev build before invoking', async () => {

packages/amplify-util-mock/src/func/index.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ export async function start(context: $TSContext): Promise<void> {
5757
printer.success('Result:');
5858
printer.info(typeof result === 'undefined' ? '' : stringResult);
5959
} catch (err) {
60-
printer.error(`${resourceName} failed with the following error:`);
61-
printer.info(err);
60+
printer.error(`${resourceName} failed with the following error:`, err);
6261
} finally {
6362
printer.info('Finished execution.');
6463
}

0 commit comments

Comments
 (0)