Skip to content

Commit b8f99d1

Browse files
committed
Keep error static properties
1 parent 0c0715d commit b8f99d1

File tree

11 files changed

+5828
-264
lines changed

11 files changed

+5828
-264
lines changed

package-lock.json

Lines changed: 5773 additions & 235 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/error/main.js

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
11
import { inspect } from 'util'
22

33
import { getMessage } from './message.js'
4-
import { getStack } from './stack.js'
54
import { printError } from './print.js'
5+
import { getEventProps } from './props.js'
6+
import { getStack } from './stack.js'
67

78
const { custom } = inspect
89

910
// Retrieve `error` which sums up all information that can be gathered about
1011
// the event.
1112
export const getError = function({ name, event }) {
13+
const { stack, ...staticProps } = getEventProps(event)
1214
const message = getMessage({ event, name })
13-
const stack = getStack({ event })
14-
const error = buildError({ name, message, stack })
15-
return { error, stack }
15+
const stackA = getStack(stack)
16+
const error = buildError({ name, message, stack: stackA, staticProps })
17+
return { error, stack: stackA }
1618
}
1719

18-
const buildError = function({ name, message, stack }) {
20+
const buildError = function({ name, message, stack, staticProps }) {
1921
const error = new Error(message)
22+
// eslint-disable-next-line fp/no-mutating-assign
23+
Object.assign(error, staticProps)
2024
// `error.name` should not be enumerable, to ensure it is correctly printed.
2125
// eslint-disable-next-line fp/no-mutating-methods
2226
Object.defineProperty(error, 'name', {

src/error/props.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// If event is an error, retrieve static properties except `name` and `message`
2+
export const getEventProps = function({ nextValue, value }) {
3+
if (nextValue instanceof Error) {
4+
return getErrorProps(nextValue)
5+
}
6+
7+
if (value instanceof Error) {
8+
return getErrorProps(value)
9+
}
10+
11+
return {}
12+
}
13+
14+
const getErrorProps = function(error) {
15+
return { stack: error.stack, ...error }
16+
}

src/error/stack.js

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,7 @@
11
// Retrieve `error.stack` by re-using the original error's stack trace
2-
export const getStack = function({ event: { nextValue, value } }) {
3-
const stack = getEventStack({ nextValue, value })
4-
const stackA = removeStackHeader({ stack })
5-
return stackA
6-
}
7-
8-
// Find the original error's stack trace
9-
const getEventStack = function({ nextValue, value }) {
10-
if (nextValue instanceof Error) {
11-
return nextValue.stack
12-
}
13-
14-
if (value instanceof Error) {
15-
return value.stack
16-
}
17-
18-
return ''
19-
}
20-
212
// Remove first line of `Error.stack` as it contains `Error.name|message`,
223
// which is already present in the upper error's `message`
23-
const removeStackHeader = function({ stack }) {
4+
export const getStack = function(stack = '') {
245
return stack.replace(FIRST_LINE_REGEXP, '')
256
}
267

test/log.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const snapshotArgs = function([error, level]) {
1616
return [
1717
normalizeMessage(inspect(error), { colors: false }),
1818
String(error),
19+
Object.keys(error),
1920
level,
2021
]
2122
}

test/options/snapshots/testing.js.md

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,6 +1267,7 @@ Generated by [AVA](https://ava.li).
12671267
Uncaught exception: Warning: WarningType: message␊
12681268
[500] Detail␊
12691269
Stack:␊
1270+
error properties: Object({ code: '500', detail: 'Detail' })␊
12701271
at STACK TRACE␊
12711272
12721273
1 spec, 1 failure␊
@@ -1328,6 +1329,8 @@ Generated by [AVA](https://ava.li).
13281329
[500] Detail␊
13291330
STACK TRACE␊
13301331
type: Warning␊
1332+
code: "500"␊
1333+
detail: Detail␊
13311334
tapCaught: uncaughtException␊
13321335
test: TAP␊
13331336
...␊
@@ -1384,8 +1387,8 @@ Generated by [AVA](https://ava.li).
13841387
expected: |-␊
13851388
undefined␊
13861389
actual: |-␊
1387-
[Warning: WarningType: message␊
1388-
[500] Detail]␊
1390+
{ [Warning: WarningType: message␊
1391+
[500] Detail] code: '500', detail: 'Detail' }
13891392
stack: |-␊
13901393
Warning: WarningType: message␊
13911394
[500] Detail␊
@@ -2036,6 +2039,7 @@ Generated by [AVA](https://ava.li).
20362039
Uncaught exception: Warning: WarningType: message␊
20372040
[500] Detail␊
20382041
Stack:␊
2042+
error properties: Object({ code: '500', detail: 'Detail' })␊
20392043
at STACK TRACE␊
20402044
20412045
1 spec, 1 failure␊
@@ -2097,6 +2101,8 @@ Generated by [AVA](https://ava.li).
20972101
[500] Detail␊
20982102
STACK TRACE␊
20992103
type: Warning␊
2104+
code: "500"␊
2105+
detail: Detail␊
21002106
tapCaught: uncaughtException␊
21012107
test: TAP␊
21022108
...␊
@@ -2153,8 +2159,8 @@ Generated by [AVA](https://ava.li).
21532159
expected: |-␊
21542160
undefined␊
21552161
actual: |-␊
2156-
[Warning: WarningType: message␊
2157-
[500] Detail]␊
2162+
{ [Warning: WarningType: message␊
2163+
[500] Detail] code: '500', detail: 'Detail' }
21582164
stack: |-␊
21592165
Warning: WarningType: message␊
21602166
[500] Detail␊
55 Bytes
Binary file not shown.

test/snapshots/log.js.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ Generated by [AVA](https://ava.li).
1616
`MultipleResolves: a promise was resolved/rejected multiple times:␊
1717
Initially resolved with: { success: true }␊
1818
Then rejected with: Error: message`,
19+
[],
1920
'info',
2021
`i multipleResolves (a promise was resolved multiple times) ␊
2122
Initially resolved with: { success: true }␊
2223
Then resolved again with: { success: true }`,
2324
`MultipleResolves: a promise was resolved multiple times:␊
2425
Initially resolved with: { success: true }␊
2526
Then resolved again with: { success: true }`,
27+
[],
2628
'info',
2729
`i multipleResolves (a promise was multiple times) ␊
2830
Initially rejected with: Error: message␊
@@ -31,6 +33,7 @@ Generated by [AVA](https://ava.li).
3133
`MultipleResolves: a promise was resolved/rejected multiple times:␊
3234
Initially rejected with: Error: message␊
3335
Then resolved with: { success: true }`,
36+
[],
3437
'info',
3538
`i multipleResolves (a promise was rejected multiple times) ␊
3639
Initially rejected with: Error: message␊
@@ -39,6 +42,7 @@ Generated by [AVA](https://ava.li).
3942
`MultipleResolves: a promise was rejected multiple times:␊
4043
Initially rejected with: Error: message␊
4144
Then rejected again with: Error: message`,
45+
[],
4246
'info',
4347
]
4448

@@ -50,6 +54,7 @@ Generated by [AVA](https://ava.li).
5054
`× rejectionHandled (a promise was rejected and handled too late) Error: message␊
5155
at STACK TRACE`,
5256
'RejectionHandled: a promise was rejected and handled too late: Error: message',
57+
[],
5358
'error',
5459
]
5560

@@ -61,6 +66,7 @@ Generated by [AVA](https://ava.li).
6166
`× uncaughtException (an exception was thrown but not caught) Error: message␊
6267
at STACK TRACE`,
6368
'UncaughtException: an exception was thrown but not caught: Error: message',
69+
[],
6470
'error',
6571
]
6672

@@ -72,6 +78,7 @@ Generated by [AVA](https://ava.li).
7278
`× unhandledRejection (a promise was rejected but not handled) Error: message␊
7379
at STACK TRACE`,
7480
'UnhandledRejection: a promise was rejected but not handled: Error: message',
81+
[],
7582
'error',
7683
]
7784

@@ -85,21 +92,32 @@ Generated by [AVA](https://ava.li).
8592
at STACK TRACE`,
8693
`Warning: WarningType: message␊
8794
[500] Detail`,
95+
[
96+
'code',
97+
'detail',
98+
],
8899
'warn',
89100
`‼ warning (WarningType1) message␊
90101
[500] ␊
91102
at STACK TRACE`,
92103
`Warning: WarningType1: message␊
93104
[500] `,
105+
[
106+
'code',
107+
],
94108
'warn',
95109
`‼ warning (WarningType2) message␊
96110
Detail␊
97111
at STACK TRACE`,
98112
`Warning: WarningType2: message␊
99113
Detail`,
114+
[
115+
'detail',
116+
],
100117
'warn',
101118
`‼ warning (WarningType3) message␊
102119
at STACK TRACE`,
103120
'Warning: WarningType3: message',
121+
[],
104122
'warn',
105123
]

test/snapshots/log.js.snap

159 Bytes
Binary file not shown.

test/snapshots/register.js.snap

1 Byte
Binary file not shown.

0 commit comments

Comments
 (0)