Skip to content

Commit

Permalink
fix: output error stacktraces in debug mode (#160)
Browse files Browse the repository at this point in the history
* chore: add test case to output stack traces

* fix: add error stacktraces to debug
  • Loading branch information
byCedric authored Feb 14, 2022
1 parent 3f293ee commit 6931e3c
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 7 deletions.
11 changes: 9 additions & 2 deletions build/preview-comment/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16021,9 +16021,16 @@ Object.defineProperty(exports, "findTool", ({ enumerable: true, get: function ()
Object.defineProperty(exports, "cacheTool", ({ enumerable: true, get: function () { return tool_cache_1.cacheDir; } }));
/**
* Auto-execute the action and pass errors to 'core.setFailed'.
* It also passes the full error, with stacktrace, to 'core.debug'.
* You'll need to enable debugging to view these full errors.
*
* @see https://github.com/actions/toolkit/blob/main/docs/action-debugging.md#step-debug-logs
*/
async function executeAction(action) {
return action().catch(error => (0, core_1.setFailed)(error.message || error));
function executeAction(action) {
return action().catch((error) => {
(0, core_1.setFailed)(error.message || error);
(0, core_1.debug)(error.stack || 'No stacktrace available');
});
}
exports.executeAction = executeAction;
/**
Expand Down
11 changes: 9 additions & 2 deletions build/setup/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67067,9 +67067,16 @@ Object.defineProperty(exports, "findTool", ({ enumerable: true, get: function ()
Object.defineProperty(exports, "cacheTool", ({ enumerable: true, get: function () { return tool_cache_1.cacheDir; } }));
/**
* Auto-execute the action and pass errors to 'core.setFailed'.
* It also passes the full error, with stacktrace, to 'core.debug'.
* You'll need to enable debugging to view these full errors.
*
* @see https://github.com/actions/toolkit/blob/main/docs/action-debugging.md#step-debug-logs
*/
async function executeAction(action) {
return action().catch(error => (0, core_1.setFailed)(error.message || error));
function executeAction(action) {
return action().catch((error) => {
(0, core_1.setFailed)(error.message || error);
(0, core_1.debug)(error.stack || 'No stacktrace available');
});
}
exports.executeAction = executeAction;
/**
Expand Down
13 changes: 10 additions & 3 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addPath, info, setFailed, warning } from '@actions/core';
import { addPath, debug, info, setFailed, warning } from '@actions/core';
import { exec } from '@actions/exec';
import { ok as assert } from 'assert';
import os from 'os';
Expand All @@ -8,9 +8,16 @@ export { find as findTool, cacheDir as cacheTool } from '@actions/tool-cache';

/**
* Auto-execute the action and pass errors to 'core.setFailed'.
* It also passes the full error, with stacktrace, to 'core.debug'.
* You'll need to enable debugging to view these full errors.
*
* @see https://github.com/actions/toolkit/blob/main/docs/action-debugging.md#step-debug-logs
*/
export async function executeAction(action: () => Promise<void>) {
return action().catch(error => setFailed(error.message || error));
export function executeAction(action: () => Promise<void>) {
return action().catch((error: Error) => {
setFailed(error.message || error);
debug(error.stack || 'No stacktrace available');
});
}

/**
Expand Down
15 changes: 15 additions & 0 deletions tests/worker.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,4 +92,19 @@ describe(executeAction, () => {
await expect(executeAction(action)).resolves.not.toThrow();
expect(core.setFailed).toBeCalledWith(error.message);
});

it('provides full stacktrace to debug', async () => {
const error = new Error('fake error');
const action = jest.fn(() => Promise.reject(error));
await expect(executeAction(action)).resolves.not.toThrow();
expect(core.debug).toBeCalledWith(error.stack);
});

it('provides fallback stacktrace to debug', async () => {
const error = new Error('fake error');
error.stack = undefined;
const action = jest.fn(() => Promise.reject(error));
await expect(executeAction(action)).resolves.not.toThrow();
expect(core.debug).toBeCalledWith('No stacktrace available');
});
});

0 comments on commit 6931e3c

Please sign in to comment.