Skip to content

Commit e17ef1b

Browse files
committed
Refactoring
1 parent b8f99d1 commit e17ef1b

File tree

5 files changed

+19
-22
lines changed

5 files changed

+19
-22
lines changed

src/error/main.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,35 @@ import { inspect } from 'util'
22

33
import { getMessage } from './message.js'
44
import { printError } from './print.js'
5-
import { getEventProps } from './props.js'
65
import { getStack } from './stack.js'
76

87
const { custom } = inspect
98

109
// Retrieve `error` which sums up all information that can be gathered about
1110
// the event.
1211
export const getError = function({ name, event }) {
13-
const { stack, ...staticProps } = getEventProps(event)
1412
const message = getMessage({ event, name })
15-
const stackA = getStack(stack)
13+
const mainValue = getMainValue(event)
14+
const staticProps = getEventProps(mainValue)
15+
const stackA = getStack(mainValue)
1616
const error = buildError({ name, message, stack: stackA, staticProps })
1717
return { error, stack: stackA }
1818
}
1919

20+
// Retrieve main thrown value, which is most likely an `Error` instance
21+
const getMainValue = function({ value, nextValue: mainValue = value }) {
22+
return mainValue
23+
}
24+
25+
// If event is an error, retrieve static properties except `name` and `message`
26+
const getEventProps = function(mainValue) {
27+
if (mainValue instanceof Error) {
28+
return { ...mainValue }
29+
}
30+
31+
return {}
32+
}
33+
2034
const buildError = function({ name, message, stack, staticProps }) {
2135
const error = new Error(message)
2236
// eslint-disable-next-line fp/no-mutating-assign

src/error/props.js

Lines changed: 0 additions & 16 deletions
This file was deleted.

src/error/stack.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Retrieve `error.stack` by re-using the original error's stack trace
22
// Remove first line of `Error.stack` as it contains `Error.name|message`,
33
// which is already present in the upper error's `message`
4-
export const getStack = function(stack = '') {
4+
export const getStack = function({ stack = '' }) {
55
return stack.replace(FIRST_LINE_REGEXP, '')
66
}
77

test/snapshots/log.js.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ Generated by [AVA](https://ava.li).
2828
'info',
2929
`i multipleResolves (a promise was multiple times) ␊
3030
Initially rejected with: Error: message␊
31-
Then resolved with: { success: true }␊
32-
at STACK TRACE`,
31+
Then resolved with: { success: true }`,
3332
`MultipleResolves: a promise was resolved/rejected multiple times:␊
3433
Initially rejected with: Error: message␊
3534
Then resolved with: { success: true }`,

test/snapshots/log.js.snap

1 Byte
Binary file not shown.

0 commit comments

Comments
 (0)