mirror of
https://github.com/actions/configure-pages.git
synced 2026-03-31 02:24:52 +00:00
Add error-utils helper and tests
This commit is contained in:
27
package-lock.json
generated
27
package-lock.json
generated
@@ -11,6 +11,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.10.1",
|
"@actions/core": "^1.10.1",
|
||||||
"@actions/github": "^6.0.0",
|
"@actions/github": "^6.0.0",
|
||||||
|
"error-stack-parser": "^2.1.4",
|
||||||
"espree": "^9.6.1"
|
"espree": "^9.6.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
@@ -2793,6 +2794,14 @@
|
|||||||
"is-arrayish": "^0.2.1"
|
"is-arrayish": "^0.2.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/error-stack-parser": {
|
||||||
|
"version": "2.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/error-stack-parser/-/error-stack-parser-2.1.4.tgz",
|
||||||
|
"integrity": "sha512-Sk5V6wVazPhq5MhpO+AUxJn5x7XSXGl1R93Vn7i+zS15KDVxQijejNCrz8340/2bgLBjR9GtEG8ZVKONDjcqGQ==",
|
||||||
|
"dependencies": {
|
||||||
|
"stackframe": "^1.3.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/es-abstract": {
|
"node_modules/es-abstract": {
|
||||||
"version": "1.21.1",
|
"version": "1.21.1",
|
||||||
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.21.1.tgz",
|
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.21.1.tgz",
|
||||||
@@ -6145,6 +6154,11 @@
|
|||||||
"node": ">=10"
|
"node": ">=10"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/stackframe": {
|
||||||
|
"version": "1.3.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/stackframe/-/stackframe-1.3.4.tgz",
|
||||||
|
"integrity": "sha512-oeVtt7eWQS+Na6F//S4kJ2K2VbRlS9D43mAlMyVpVWovy9o+jfgH8O9agzANzaiLjclA0oYzUXEM4PurhSUChw=="
|
||||||
|
},
|
||||||
"node_modules/stop-iteration-iterator": {
|
"node_modules/stop-iteration-iterator": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/stop-iteration-iterator/-/stop-iteration-iterator-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/stop-iteration-iterator/-/stop-iteration-iterator-1.0.0.tgz",
|
||||||
@@ -8805,6 +8819,14 @@
|
|||||||
"is-arrayish": "^0.2.1"
|
"is-arrayish": "^0.2.1"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"error-stack-parser": {
|
||||||
|
"version": "2.1.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/error-stack-parser/-/error-stack-parser-2.1.4.tgz",
|
||||||
|
"integrity": "sha512-Sk5V6wVazPhq5MhpO+AUxJn5x7XSXGl1R93Vn7i+zS15KDVxQijejNCrz8340/2bgLBjR9GtEG8ZVKONDjcqGQ==",
|
||||||
|
"requires": {
|
||||||
|
"stackframe": "^1.3.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"es-abstract": {
|
"es-abstract": {
|
||||||
"version": "1.21.1",
|
"version": "1.21.1",
|
||||||
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.21.1.tgz",
|
"resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.21.1.tgz",
|
||||||
@@ -11242,6 +11264,11 @@
|
|||||||
"escape-string-regexp": "^2.0.0"
|
"escape-string-regexp": "^2.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"stackframe": {
|
||||||
|
"version": "1.3.4",
|
||||||
|
"resolved": "https://registry.npmjs.org/stackframe/-/stackframe-1.3.4.tgz",
|
||||||
|
"integrity": "sha512-oeVtt7eWQS+Na6F//S4kJ2K2VbRlS9D43mAlMyVpVWovy9o+jfgH8O9agzANzaiLjclA0oYzUXEM4PurhSUChw=="
|
||||||
|
},
|
||||||
"stop-iteration-iterator": {
|
"stop-iteration-iterator": {
|
||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/stop-iteration-iterator/-/stop-iteration-iterator-1.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/stop-iteration-iterator/-/stop-iteration-iterator-1.0.0.tgz",
|
||||||
|
|||||||
@@ -26,6 +26,7 @@
|
|||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@actions/core": "^1.10.1",
|
"@actions/core": "^1.10.1",
|
||||||
"@actions/github": "^6.0.0",
|
"@actions/github": "^6.0.0",
|
||||||
|
"error-stack-parser": "^2.1.4",
|
||||||
"espree": "^9.6.1"
|
"espree": "^9.6.1"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
|||||||
24
src/error-utils.js
Normal file
24
src/error-utils.js
Normal file
@@ -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 }
|
||||||
38
src/error-utils.test.js
Normal file
38
src/error-utils.test.js
Normal file
@@ -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)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in New Issue
Block a user