-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #138 from actions/error-utils
Convert errors into Actions-compatible logging with annotations
- Loading branch information
Showing
10 changed files
with
574 additions
and
31 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
const ErrorStackParser = require('error-stack-parser') | ||
|
||
// Convert an Error's stack into `@actions/core` toolkit AnnotationProperties: | ||
// https://github.com/actions/toolkit/blob/ef77c9d60bdb03700d7758b0d04b88446e72a896/packages/core/src/core.ts#L36-L71 | ||
function convertErrorToAnnotationProperties(error, title = error.name) { | ||
if (!(error instanceof Error)) { | ||
throw new TypeError('error must be an instance of Error') | ||
} | ||
|
||
const stack = ErrorStackParser.parse(error) | ||
const firstFrame = stack && stack.length > 0 ? stack[0] : null | ||
if (!firstFrame) { | ||
throw new Error('Error stack is empty or unparseable') | ||
} | ||
|
||
return { | ||
title, | ||
file: firstFrame.fileName, | ||
startLine: firstFrame.lineNumber, | ||
startColumn: firstFrame.columnNumber | ||
} | ||
} | ||
|
||
module.exports = { convertErrorToAnnotationProperties } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const { convertErrorToAnnotationProperties } = require('./error-utils') | ||
|
||
describe('error-utils', () => { | ||
describe('convertErrorToAnnotationProperties', () => { | ||
it('throws a TypeError if the first argument is not an Error instance', () => { | ||
expect(() => convertErrorToAnnotationProperties('not an Error')).toThrow( | ||
TypeError, | ||
'error must be an instance of Error' | ||
) | ||
}) | ||
|
||
it('throws an Error if the first argument is an Error instance without a parseable stack', () => { | ||
const error = new Error('Test error') | ||
error.stack = '' | ||
expect(() => convertErrorToAnnotationProperties(error)).toThrow(Error, 'Error stack is empty or unparseable') | ||
}) | ||
|
||
it('returns an AnnotationProperties-compatible object', () => { | ||
const result = convertErrorToAnnotationProperties(new TypeError('Test error')) | ||
expect(result).toEqual({ | ||
title: 'TypeError', | ||
file: __filename, | ||
startLine: expect.any(Number), | ||
startColumn: expect.any(Number) | ||
}) | ||
}) | ||
|
||
it('returns an AnnotationProperties-compatible object with a custom title', () => { | ||
const result = convertErrorToAnnotationProperties(new TypeError('Test error'), 'custom title') | ||
expect(result).toEqual({ | ||
title: 'custom title', | ||
file: __filename, | ||
startLine: expect.any(Number), | ||
startColumn: expect.any(Number) | ||
}) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters