|
| 1 | +import { after, before, beforeEach, describe, it, mock } from 'node:test'; |
| 2 | +import { |
| 3 | + attachUnhandledExceptionListeners, |
| 4 | + generateCommandFailureHandler, |
| 5 | +} from './error_handler.js'; |
| 6 | +import { LogLevel, printer } from '@aws-amplify/cli-core'; |
| 7 | +import assert from 'node:assert'; |
| 8 | +import { AmplifyUserError } from '@aws-amplify/platform-core'; |
| 9 | + |
| 10 | +const mockPrint = mock.method(printer, 'print'); |
| 11 | +const mockLog = mock.method(printer, 'log'); |
| 12 | + |
| 13 | +void describe('generateCommandFailureHandler', () => { |
| 14 | + beforeEach(() => { |
| 15 | + mockPrint.mock.resetCalls(); |
| 16 | + mockLog.mock.resetCalls(); |
| 17 | + }); |
| 18 | + |
| 19 | + void it('prints specified message with undefined error', async () => { |
| 20 | + const someMsg = 'some msg'; |
| 21 | + // undefined error is encountered with --help option. |
| 22 | + await generateCommandFailureHandler()( |
| 23 | + someMsg, |
| 24 | + undefined as unknown as Error |
| 25 | + ); |
| 26 | + assert.equal(mockPrint.mock.callCount(), 1); |
| 27 | + assert.match(mockPrint.mock.calls[0].arguments[0], new RegExp(someMsg)); |
| 28 | + }); |
| 29 | + |
| 30 | + void it('prints message from error object', async () => { |
| 31 | + const errMsg = 'some error msg'; |
| 32 | + await generateCommandFailureHandler()('', new Error(errMsg)); |
| 33 | + assert.equal(mockPrint.mock.callCount(), 1); |
| 34 | + assert.match( |
| 35 | + mockPrint.mock.calls[0].arguments[0] as string, |
| 36 | + new RegExp(errMsg) |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + void it('handles a prompt force close error', async () => { |
| 41 | + await generateCommandFailureHandler()( |
| 42 | + '', |
| 43 | + new Error('User force closed the prompt') |
| 44 | + ); |
| 45 | + assert.equal(mockPrint.mock.callCount(), 0); |
| 46 | + }); |
| 47 | + |
| 48 | + void it('prints error cause message, if any', async () => { |
| 49 | + const errorMessage = 'this is the upstream cause'; |
| 50 | + await generateCommandFailureHandler()( |
| 51 | + '', |
| 52 | + new Error('some error msg', { cause: new Error(errorMessage) }) |
| 53 | + ); |
| 54 | + assert.equal(mockPrint.mock.callCount(), 2); |
| 55 | + assert.match( |
| 56 | + mockPrint.mock.calls[1].arguments[0] as string, |
| 57 | + new RegExp(errorMessage) |
| 58 | + ); |
| 59 | + }); |
| 60 | + |
| 61 | + void it('prints AmplifyErrors', async () => { |
| 62 | + await generateCommandFailureHandler()( |
| 63 | + '', |
| 64 | + new AmplifyUserError('TestNameError', { |
| 65 | + message: 'test error message', |
| 66 | + resolution: 'test resolution', |
| 67 | + details: 'test details', |
| 68 | + }) |
| 69 | + ); |
| 70 | + |
| 71 | + assert.equal(mockPrint.mock.callCount(), 3); |
| 72 | + assert.match( |
| 73 | + mockPrint.mock.calls[0].arguments[0], |
| 74 | + /TestNameError: test error message/ |
| 75 | + ); |
| 76 | + assert.equal( |
| 77 | + mockPrint.mock.calls[1].arguments[0], |
| 78 | + 'Resolution: test resolution' |
| 79 | + ); |
| 80 | + assert.equal(mockPrint.mock.calls[2].arguments[0], 'Details: test details'); |
| 81 | + }); |
| 82 | + |
| 83 | + void it('prints debug stack traces', async () => { |
| 84 | + const causeError = new Error('test underlying cause error'); |
| 85 | + const amplifyError = new AmplifyUserError( |
| 86 | + 'TestNameError', |
| 87 | + { |
| 88 | + message: 'test error message', |
| 89 | + resolution: 'test resolution', |
| 90 | + details: 'test details', |
| 91 | + }, |
| 92 | + causeError |
| 93 | + ); |
| 94 | + await generateCommandFailureHandler()('', amplifyError); |
| 95 | + assert.equal(mockLog.mock.callCount(), 2); |
| 96 | + assert.deepStrictEqual(mockLog.mock.calls[0].arguments, [ |
| 97 | + amplifyError.stack, |
| 98 | + LogLevel.DEBUG, |
| 99 | + ]); |
| 100 | + assert.deepStrictEqual(mockLog.mock.calls[1].arguments, [ |
| 101 | + causeError.stack, |
| 102 | + LogLevel.DEBUG, |
| 103 | + ]); |
| 104 | + }); |
| 105 | +}); |
| 106 | + |
| 107 | +void describe( |
| 108 | + 'attachUnhandledExceptionListeners', |
| 109 | + { concurrency: 1 }, |
| 110 | + async () => { |
| 111 | + before(async () => { |
| 112 | + attachUnhandledExceptionListeners(); |
| 113 | + }); |
| 114 | + |
| 115 | + beforeEach(() => { |
| 116 | + mockPrint.mock.resetCalls(); |
| 117 | + }); |
| 118 | + |
| 119 | + after(() => { |
| 120 | + // remove the exception listeners that were added during setup |
| 121 | + process.listeners('unhandledRejection').pop(); |
| 122 | + process.listeners('uncaughtException').pop(); |
| 123 | + }); |
| 124 | + void it('handles rejected errors', () => { |
| 125 | + process.listeners('unhandledRejection').at(-1)?.( |
| 126 | + new Error('test error'), |
| 127 | + Promise.resolve() |
| 128 | + ); |
| 129 | + assert.ok( |
| 130 | + mockPrint.mock.calls.findIndex((call) => |
| 131 | + call.arguments[0].includes('test error') |
| 132 | + ) >= 0 |
| 133 | + ); |
| 134 | + expectProcessExitCode1AndReset(); |
| 135 | + }); |
| 136 | + |
| 137 | + void it('handles rejected strings', () => { |
| 138 | + process.listeners('unhandledRejection').at(-1)?.( |
| 139 | + 'test error', |
| 140 | + Promise.resolve() |
| 141 | + ); |
| 142 | + assert.ok( |
| 143 | + mockPrint.mock.calls.findIndex((call) => |
| 144 | + call.arguments[0].includes('test error') |
| 145 | + ) >= 0 |
| 146 | + ); |
| 147 | + expectProcessExitCode1AndReset(); |
| 148 | + }); |
| 149 | + |
| 150 | + void it('handles rejected symbols of other types', () => { |
| 151 | + process.listeners('unhandledRejection').at(-1)?.( |
| 152 | + { something: 'weird' }, |
| 153 | + Promise.resolve() |
| 154 | + ); |
| 155 | + assert.ok( |
| 156 | + mockPrint.mock.calls.findIndex((call) => |
| 157 | + call.arguments[0].includes( |
| 158 | + 'Error: Unhandled rejection of type [object]' |
| 159 | + ) |
| 160 | + ) >= 0 |
| 161 | + ); |
| 162 | + expectProcessExitCode1AndReset(); |
| 163 | + }); |
| 164 | + |
| 165 | + void it('handles uncaught errors', () => { |
| 166 | + process.listeners('uncaughtException').at(-1)?.( |
| 167 | + new Error('test error'), |
| 168 | + 'uncaughtException' |
| 169 | + ); |
| 170 | + assert.ok( |
| 171 | + mockPrint.mock.calls.findIndex((call) => |
| 172 | + call.arguments[0].includes('test error') |
| 173 | + ) >= 0 |
| 174 | + ); |
| 175 | + expectProcessExitCode1AndReset(); |
| 176 | + }); |
| 177 | + |
| 178 | + void it('does nothing when called multiple times', () => { |
| 179 | + // note the first call happened in the before() setup |
| 180 | + |
| 181 | + const unhandledRejectionListenerCount = |
| 182 | + process.listenerCount('unhandledRejection'); |
| 183 | + const uncaughtExceptionListenerCount = |
| 184 | + process.listenerCount('uncaughtException'); |
| 185 | + |
| 186 | + attachUnhandledExceptionListeners(); |
| 187 | + attachUnhandledExceptionListeners(); |
| 188 | + |
| 189 | + assert.equal( |
| 190 | + process.listenerCount('unhandledRejection'), |
| 191 | + unhandledRejectionListenerCount |
| 192 | + ); |
| 193 | + assert.equal( |
| 194 | + process.listenerCount('uncaughtException'), |
| 195 | + uncaughtExceptionListenerCount |
| 196 | + ); |
| 197 | + }); |
| 198 | + } |
| 199 | +); |
| 200 | + |
| 201 | +const expectProcessExitCode1AndReset = () => { |
| 202 | + assert.equal(process.exitCode, 1); |
| 203 | + process.exitCode = 0; |
| 204 | +}; |
0 commit comments