Skip to content

Commit 48e663d

Browse files
committed
Pass original error to log() option
1 parent 1389065 commit 48e663d

File tree

7 files changed

+51
-8
lines changed

7 files changed

+51
-8
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ other `require`/`import` statements.
9999

100100
## log
101101

102-
_Type_: `function(error, level)`
102+
_Type_: `function(error, level, originalError)`
103103

104104
Customizes how process errors are logged.\
105105
[Full documentation](docs/API.md#log).

docs/API.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ _Type_: `object`
4242

4343
#### log
4444

45-
_Type_: `function(error, level)`
45+
_Type_: `function(error, level, originalError)`
4646

4747
By default process errors will be logged to the console using `console.error()`,
4848
`console.warn()`, etc.
@@ -52,13 +52,14 @@ process errors with [Winston](https://github.com/winstonjs/winston) instead:
5252

5353
```js
5454
logProcessErrors({
55-
log(error, level) {
55+
log(error, level, originalError) {
5656
winstonLogger[level](error.stack)
5757
},
5858
})
5959
```
6060

61-
The function's arguments are [`error`](#error) and [`level`](#level).
61+
The function's arguments are [`error`](#error), [`level`](#level) and
62+
[`originalError`](#error).
6263

6364
If logging is asynchronous, the function should return a promise (or use
6465
`async`/`await`). This is not necessary if logging is using streams (like
@@ -215,6 +216,10 @@ _Type_: `Error`
215216
The [`log`](#log) and [`level`](#level) options receive as argument an `error`
216217
instance.
217218

219+
This error is generated based on the original process error but with an improved
220+
`name`, `message` and `stack`. However the original process error is still
221+
available as a third argument to [`log`](#log).
222+
218223
#### error.name
219224

220225
_Type_: `string`\

src/error/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const getError = function ({ name, event }) {
1414
const staticProps = getEventProps(mainValue)
1515
const stackA = getStack(mainValue)
1616
const error = buildError({ name, message, stack: stackA, staticProps })
17-
return { error, stack: stackA }
17+
return { error, stack: stackA, mainValue }
1818
}
1919

2020
// Retrieve main thrown value, which is most likely an `Error` instance

src/handle/common.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const handleEvent = async function ({
3939
}
4040

4141
const logEvent = async function ({ opts, name, event }) {
42-
const { error, stack } = getError({ name, event })
42+
const { error, stack, mainValue } = getError({ name, event })
4343

4444
const level = getLevel({ opts, name, error })
4545

@@ -50,5 +50,5 @@ const logEvent = async function ({ opts, name, event }) {
5050
addErrorPrint({ error, opts, level, name, stack })
5151

5252
// See `exit.js` on why we need to `await`
53-
await opts.log(error, level)
53+
await opts.log(error, level, mainValue)
5454
}

test/log.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import { removeProcessListeners } from './helpers/remove.js'
1212

1313
removeProcessListeners()
1414

15-
const snapshotArgs = function ([error, level]) {
15+
const snapshotArgs = function ([error, level, mainValue]) {
1616
return [
1717
normalizeMessage(inspect(error), { colors: false }),
1818
String(error),
1919
Object.keys(error),
2020
level,
21+
mainValue,
2122
]
2223
}
2324

test/snapshots/log.js.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ Generated by [AVA](https://avajs.dev).
1818
Then rejected with: Error: message`,
1919
[],
2020
'info',
21+
Error {
22+
message: 'message',
23+
},
2124
`i multipleResolves (a promise was resolved multiple times) ␊
2225
Initially resolved with: { success: true }␊
2326
Then resolved again with: { success: true }`,
@@ -26,6 +29,9 @@ Generated by [AVA](https://avajs.dev).
2629
Then resolved again with: { success: true }`,
2730
[],
2831
'info',
32+
{
33+
success: true,
34+
},
2935
`i multipleResolves (a promise was multiple times) ␊
3036
Initially rejected with: Error: message␊
3137
Then resolved with: { success: true }`,
@@ -34,6 +40,9 @@ Generated by [AVA](https://avajs.dev).
3440
Then resolved with: { success: true }`,
3541
[],
3642
'info',
43+
{
44+
success: true,
45+
},
3746
`i multipleResolves (a promise was rejected multiple times) ␊
3847
Initially rejected with: Error: message␊
3948
Then rejected again with: Error: message␊
@@ -43,6 +52,9 @@ Generated by [AVA](https://avajs.dev).
4352
Then rejected again with: Error: message`,
4453
[],
4554
'info',
55+
Error {
56+
message: 'message',
57+
},
4658
]
4759

4860
## should fire opts.log() with arguments | rejectionHandled
@@ -55,6 +67,9 @@ Generated by [AVA](https://avajs.dev).
5567
'RejectionHandled: a promise was rejected and handled too late: Error: message',
5668
[],
5769
'error',
70+
Error {
71+
message: 'message',
72+
},
5873
]
5974

6075
## should fire opts.log() with arguments | uncaughtException
@@ -67,6 +82,9 @@ Generated by [AVA](https://avajs.dev).
6782
'UncaughtException: an exception was thrown but not caught: Error: message',
6883
[],
6984
'error',
85+
Error {
86+
message: 'message',
87+
},
7088
]
7189

7290
## should fire opts.log() with arguments | unhandledRejection
@@ -79,6 +97,9 @@ Generated by [AVA](https://avajs.dev).
7997
'UnhandledRejection: a promise was rejected but not handled: Error: message',
8098
[],
8199
'error',
100+
Error {
101+
message: 'message',
102+
},
82103
]
83104

84105
## should fire opts.log() with arguments | warning
@@ -96,6 +117,11 @@ Generated by [AVA](https://avajs.dev).
96117
'detail',
97118
],
98119
'warn',
120+
WarningType (Error) {
121+
code: '500',
122+
detail: 'Detail',
123+
message: 'message',
124+
},
99125
`‼ warning (WarningType1) message␊
100126
[500] ␊
101127
at STACK TRACE`,
@@ -105,6 +131,10 @@ Generated by [AVA](https://avajs.dev).
105131
'code',
106132
],
107133
'warn',
134+
WarningType1 (Error) {
135+
code: '500',
136+
message: 'message',
137+
},
108138
`‼ warning (WarningType2) message␊
109139
Detail␊
110140
at STACK TRACE`,
@@ -114,9 +144,16 @@ Generated by [AVA](https://avajs.dev).
114144
'detail',
115145
],
116146
'warn',
147+
WarningType2 (Error) {
148+
detail: 'Detail',
149+
message: 'message',
150+
},
117151
`‼ warning (WarningType3) message␊
118152
at STACK TRACE`,
119153
'Warning: WarningType3: message',
120154
[],
121155
'warn',
156+
WarningType3 (Error) {
157+
message: 'message',
158+
},
122159
]

test/snapshots/log.js.snap

302 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)