Skip to content

Commit f8112b6

Browse files
authored
If present, print error.cause.message in top-level error handler (#928)
1 parent 6a001bf commit f8112b6

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

.changeset/honest-pumas-fold.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@aws-amplify/backend-cli': patch
3+
---
4+
5+
If present, print error.cause.message in top-level error handler. This gives customers more debugging information when the error cause rather than the error contains the root cause of the issue.

packages/cli/src/error_handler.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,21 @@ void describe('generateCommandFailureHandler', () => {
7171
assert.match(mockPrint.mock.calls[0].arguments[0], new RegExp(errMsg));
7272
assert.equal(mockPrint.mock.calls[0].arguments[1], COLOR.RED);
7373
});
74+
75+
void it('prints error cause message, if any', () => {
76+
const errorMessage = 'this is the upstream cause';
77+
generateCommandFailureHandler(parser)(
78+
'',
79+
new Error('some error msg', { cause: new Error(errorMessage) })
80+
);
81+
assert.equal(mockExit.mock.callCount(), 1);
82+
assert.equal(mockPrint.mock.callCount(), 2);
83+
assert.match(
84+
mockPrint.mock.calls[1].arguments[0],
85+
new RegExp(errorMessage)
86+
);
87+
assert.equal(mockPrint.mock.calls[1].arguments[1], COLOR.RED);
88+
});
7489
});
7590

7691
void describe('attachUnhandledExceptionListeners', { concurrency: 1 }, () => {

packages/cli/src/error_handler.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ export const generateCommandFailureHandler = (
5050
* @param message error message set by the yargs:check validations
5151
* @param error error thrown by yargs handler
5252
*/
53-
const handleCommandFailure = (message: string, error: Error) => {
53+
const handleCommandFailure = (message: string, error?: Error) => {
5454
const printHelp = () => {
5555
Printer.printNewLine();
5656
parser.showHelp();
5757
Printer.printNewLine();
5858
};
5959

6060
handleError(error, printHelp, message);
61-
parser.exit(1, error);
61+
parser.exit(1, error || new Error(message));
6262
};
6363
return handleCommandFailure;
6464
};
@@ -71,7 +71,7 @@ export const generateCommandFailureHandler = (
7171
* (Note that we don't do all of those things yet, but this is where they should go)
7272
*/
7373
const handleError = (
74-
error: Error,
74+
error?: Error,
7575
printMessagePreamble?: () => void,
7676
message?: string
7777
) => {
@@ -87,9 +87,23 @@ const handleError = (
8787

8888
printMessagePreamble?.();
8989
Printer.print(message || String(error), COLOR.RED);
90+
if (errorHasCauseMessage(error)) {
91+
Printer.print(error.cause.message, COLOR.RED);
92+
}
9093
Printer.printNewLine();
9194
};
9295

93-
const isUserForceClosePromptError = (err: Error): boolean => {
94-
return err?.message.includes('User force closed the prompt');
96+
const isUserForceClosePromptError = (err?: Error): boolean => {
97+
return !!err && err?.message.includes('User force closed the prompt');
98+
};
99+
100+
const errorHasCauseMessage = (
101+
error?: Error
102+
): error is Error & { cause: { message: string } } => {
103+
return (
104+
typeof error?.cause === 'object' &&
105+
!!error.cause &&
106+
'message' in error.cause &&
107+
typeof error.cause.message === 'string'
108+
);
95109
};

0 commit comments

Comments
 (0)