open source is social v-it.org
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2026 sol pbc
3
4import { collectCauses } from './error-format.js';
5
6function errorMessage(input) {
7 if (input instanceof Error) return input.message || String(input);
8 return String(input);
9}
10
11export function jsonOk(data) {
12 console.log(JSON.stringify({ ok: true, ...data }, null, 2));
13}
14
15export function jsonError(input, hintArg) {
16 if (typeof input === 'string') {
17 const obj = { ok: false, error: input };
18 if (hintArg) obj.hint = hintArg;
19 console.log(JSON.stringify(obj, null, 2));
20 process.exitCode = 1;
21 return;
22 }
23
24 const obj = {
25 ok: false,
26 error: errorMessage(input),
27 };
28 const causes = collectCauses(input);
29 if (causes.length > 0) obj.causes = causes;
30
31 const resolvedHint = typeof hintArg === 'string'
32 ? hintArg
33 : (typeof input?.hint === 'string' ? input.hint : undefined);
34 if (resolvedHint) obj.hint = resolvedHint;
35
36 console.log(JSON.stringify(obj, null, 2));
37 process.exitCode = 1;
38}