Skip to content

Commit c37f296

Browse files
authored
Merge pull request #84 from actions/sgoedecke/better-error-logging
Log specific error even if it is not an Error
2 parents 4b591cc + e7ddc84 commit c37f296

File tree

4 files changed

+21
-6
lines changed

4 files changed

+21
-6
lines changed

__tests__/main.test.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ vi.mock('../src/inference.js', () => ({
9494

9595
vi.mock('@actions/core', () => core)
9696

97+
// Mock process.exit to prevent it from actually exiting during tests
98+
const mockProcessExit = vi.spyOn(process, 'exit').mockImplementation(() => {
99+
throw new Error('process.exit called')
100+
})
101+
97102
// The module being tested should be imported dynamically. This ensures that the
98103
// mocks are used in place of any actual dependencies.
99104
const {run} = await import('../src/main.js')
@@ -102,6 +107,7 @@ describe('main.ts', () => {
102107
// Reset all mocks before each test
103108
beforeEach(() => {
104109
vi.clearAllMocks()
110+
mockProcessExit.mockClear()
105111

106112
// Remove any existing GITHUB_TOKEN
107113
delete process.env.GITHUB_TOKEN
@@ -129,9 +135,11 @@ describe('main.ts', () => {
129135
'prompt-file': '',
130136
})
131137

132-
await run()
138+
// Expect the run function to throw due to process.exit being mocked
139+
await expect(run()).rejects.toThrow('process.exit called')
133140

134-
expect(core.setFailed).toHaveBeenNthCalledWith(1, 'Neither prompt-file nor prompt was set')
141+
expect(core.setFailed).toHaveBeenCalledWith('Neither prompt-file nor prompt was set')
142+
expect(mockProcessExit).toHaveBeenCalledWith(1)
135143
})
136144

137145
it('uses simple inference when MCP is disabled', async () => {
@@ -251,8 +259,10 @@ describe('main.ts', () => {
251259
'prompt-file': promptFile,
252260
})
253261

254-
await run()
262+
// Expect the run function to throw due to process.exit being mocked
263+
await expect(run()).rejects.toThrow('process.exit called')
255264

256265
expect(core.setFailed).toHaveBeenCalledWith(`File for prompt-file was not found: ${promptFile}`)
266+
expect(mockProcessExit).toHaveBeenCalledWith(1)
257267
})
258268
})

dist/index.js

Lines changed: 3 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/main.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ export async function run(): Promise<void> {
9494
if (error instanceof Error) {
9595
core.setFailed(error.message)
9696
} else {
97-
core.setFailed('An unexpected error occurred')
97+
core.setFailed(`An unexpected error occurred: ${JSON.stringify(error, null, 2)}`)
9898
}
99+
100+
// Force exit to prevent hanging on open connections
101+
process.exit(1)
99102
}
100103
}
101104

0 commit comments

Comments
 (0)