Skip to content

Commit 5e31ef6

Browse files
committed
Cleanup serializeResultJson
1 parent 2982abe commit 5e31ef6

File tree

1 file changed

+18
-24
lines changed

1 file changed

+18
-24
lines changed
Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,43 @@
11
import { debugDir, debugFn } from '@socketsecurity/registry/lib/debug'
22
import { logger } from '@socketsecurity/registry/lib/logger'
3+
import { isObject } from '@socketsecurity/registry/lib/objects'
34

45
import type { CResult } from '../types.mts'
56

67
// Serialize the final result object before printing it
78
// All commands that support the --json flag should call this before printing
89
export function serializeResultJson(data: CResult<unknown>): string {
9-
if (typeof data !== 'object' || !data) {
10+
if (!isObject(data)) {
1011
process.exitCode = 1
11-
debugFn('inspect', 'typeof data=', typeof data)
1212

13-
if (typeof data !== 'object' && data) {
14-
debugFn('inspect', 'data:\n', data)
15-
}
13+
debugFn('inspect', { data })
1614

1715
// We should not allow the JSON value to be "null", or a boolean/number/string,
1816
// even if they are valid "json".
19-
const message =
20-
'There was a problem converting the data set to JSON. The JSON was not an object. Please try again without --json'
21-
22-
return (
23-
JSON.stringify({
24-
ok: false,
25-
message: 'Unable to serialize JSON',
26-
cause: message,
27-
}).trim() + '\n'
28-
)
17+
return `${JSON.stringify({
18+
ok: false,
19+
message: 'Unable to serialize JSON',
20+
cause:
21+
'There was a problem converting the data set to JSON. The JSON was not an object. Please try again without --json',
22+
}).trim()}\n`
2923
}
3024

3125
try {
32-
return JSON.stringify(data, null, 2).trim() + '\n'
26+
return `${JSON.stringify(data, null, 2).trim()}\n`
3327
} catch (e) {
3428
process.exitCode = 1
3529

36-
// This could be caused by circular references, which is an "us" problem
3730
const message =
3831
'There was a problem converting the data set to JSON. Please try again without --json'
32+
3933
logger.fail(message)
4034
debugDir('inspect', { error: e })
41-
return (
42-
JSON.stringify({
43-
ok: false,
44-
message: 'Unable to serialize JSON',
45-
cause: message,
46-
}).trim() + '\n'
47-
)
35+
36+
// This could be caused by circular references, which is an "us" problem.
37+
return `${JSON.stringify({
38+
ok: false,
39+
message: 'Unable to serialize JSON',
40+
cause: message,
41+
}).trim()}\n`
4842
}
4943
}

0 commit comments

Comments
 (0)