+61
-18
lines changedFilter options
+61
-18
lines changed Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
1
-
function filterError (input) {
2
-
return {
3
-
errorType: input.name,
4
-
message: input.message,
5
-
stack: input.stack,
6
-
...(input.code ? { code: input.code } : {}),
7
-
...(input.statusCode ? { statusCode: input.statusCode } : {}),
8
-
}
9
-
}
1
+
const { serializeError } = require('./error')
10
2
11
3
const deepMap = (input, handler = v => v, path = ['$'], seen = new Set([input])) => {
12
4
// this is in an effort to maintain bole's error logging behavior
13
5
if (path.join('.') === '$' && input instanceof Error) {
14
-
return deepMap({ err: filterError(input) }, handler, path, seen)
6
+
return deepMap({ err: serializeError(input) }, handler, path, seen)
15
7
}
16
8
if (input instanceof Error) {
17
-
return deepMap(filterError(input), handler, path, seen)
9
+
return deepMap(serializeError(input), handler, path, seen)
18
10
}
19
11
if (input instanceof Buffer) {
20
12
return `[unable to log instanceof buffer]`
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
1
+
/** takes an error object and serializes it to a plan object */
2
+
function serializeError (input) {
3
+
if (!(input instanceof Error)) {
4
+
if (typeof input === 'string') {
5
+
const error = new Error(`attempted to serialize a non-error, string String, "${input}"`)
6
+
return serializeError(error)
7
+
}
8
+
const error = new Error(`attempted to serialize a non-error, ${typeof input} ${input?.constructor?.name}`)
9
+
return serializeError(error)
10
+
}
11
+
// different error objects store status code differently
12
+
// AxiosError uses `status`, other services use `statusCode`
13
+
const statusCode = input.statusCode ?? input.status
14
+
// CAUTION: what we serialize here gets add to the size of logs
15
+
return {
16
+
errorType: input.errorType ?? input.constructor.name,
17
+
...(input.message ? { message: input.message } : {}),
18
+
...(input.stack ? { stack: input.stack } : {}),
19
+
// think of this as error code
20
+
...(input.code ? { code: input.code } : {}),
21
+
// think of this as http status code
22
+
...(statusCode ? { statusCode } : {}),
23
+
}
24
+
}
25
+
26
+
module.exports = {
27
+
serializeError,
28
+
}
Original file line number Diff line number Diff line change
@@ -14,6 +14,8 @@ const {
14
14
redactMatchers,
15
15
} = require('./utils')
16
16
17
+
const { serializeError } = require('./error')
18
+
17
19
const { deepMap } = require('./deep-map')
18
20
19
21
const _redact = redactMatchers(
@@ -31,4 +33,25 @@ const _redact = redactMatchers(
31
33
32
34
const redact = (input) => deepMap(input, (value, path) => _redact(value, { path }))
33
35
34
-
module.exports = { redact }
36
+
/** takes an error returns new error keeping some custom properties */
37
+
function redactError (input) {
38
+
const { message, ...data } = serializeError(input)
39
+
const output = new Error(redact(message))
40
+
return Object.assign(output, redact(data))
41
+
}
42
+
43
+
/** runs a function within try / catch and throws error wrapped in redactError */
44
+
function redactThrow (func) {
45
+
if (typeof func !== 'function') {
46
+
throw new Error('redactThrow expects a function')
47
+
}
48
+
return async (...args) => {
49
+
try {
50
+
return await func(...args)
51
+
} catch (error) {
52
+
throw redactError(error)
53
+
}
54
+
}
55
+
}
56
+
57
+
module.exports = { redact, redactError, redactThrow }
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
1
1
{
2
2
"name": "@npmcli/redact",
3
-
"version": "3.0.0",
3
+
"version": "3.1.1",
4
4
"description": "Redact sensitive npm information from output",
5
5
"main": "lib/index.js",
6
6
"exports": {
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@
91
91
"@npmcli/map-workspaces": "^4.0.2",
92
92
"@npmcli/package-json": "^6.1.1",
93
93
"@npmcli/promise-spawn": "^8.0.2",
94
-
"@npmcli/redact": "^3.0.0",
94
+
"@npmcli/redact": "^3.1.1",
95
95
"@npmcli/run-script": "^9.0.1",
96
96
"@sigstore/tuf": "^3.0.0",
97
97
"abbrev": "^3.0.0",
@@ -3716,9 +3716,9 @@
3716
3716
}
3717
3717
},
3718
3718
"node_modules/@npmcli/redact": {
3719
-
"version": "3.0.0",
3720
-
"resolved": "https://registry.npmjs.org/@npmcli/redact/-/redact-3.0.0.tgz",
3721
-
"integrity": "sha512-/1uFzjVcfzqrgCeGW7+SZ4hv0qLWmKXVzFahZGJ6QuJBj6Myt9s17+JL86i76NV9YSnJRcGXJYQbAU0rn1YTCQ==",
3719
+
"version": "3.1.1",
3720
+
"resolved": "https://registry.npmjs.org/@npmcli/redact/-/redact-3.1.1.tgz",
3721
+
"integrity": "sha512-3Hc2KGIkrvJWJqTbvueXzBeZlmvoOxc2jyX00yzr3+sNFquJg0N8hH4SAPLPVrkWIRQICVpVgjrss971awXVnA==",
3722
3722
"inBundle": true,
3723
3723
"license": "ISC",
3724
3724
"engines": {
Original file line number Diff line number Diff line change
@@ -58,7 +58,7 @@
58
58
"@npmcli/map-workspaces": "^4.0.2",
59
59
"@npmcli/package-json": "^6.1.1",
60
60
"@npmcli/promise-spawn": "^8.0.2",
61
-
"@npmcli/redact": "^3.0.0",
61
+
"@npmcli/redact": "^3.1.1",
62
62
"@npmcli/run-script": "^9.0.1",
63
63
"@sigstore/tuf": "^3.0.0",
64
64
"abbrev": "^3.0.0",
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4