@4ge/cli 0.2.2 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base.js +42 -11
- package/package.json +1 -1
package/dist/base.js
CHANGED
|
@@ -177,21 +177,47 @@ export class BaseCommand extends Command {
|
|
|
177
177
|
},
|
|
178
178
|
}, null, 2));
|
|
179
179
|
}
|
|
180
|
+
/**
|
|
181
|
+
* Render an error respecting the active output mode.
|
|
182
|
+
*
|
|
183
|
+
* - JSON mode: emit the structured `{ data, error, metadata }` envelope.
|
|
184
|
+
* - Pretty mode: render a human-readable error (never a JSON blob) using
|
|
185
|
+
* oclif's `error()`, which pretty-prints to stderr and exits non-zero.
|
|
186
|
+
*
|
|
187
|
+
* Centralising the mode branch here means command authors can call
|
|
188
|
+
* `outputErrorAndExit({ message, code, hint })` without guarding on
|
|
189
|
+
* `isJsonMode` themselves — eliminating a class of bug where pretty-mode
|
|
190
|
+
* users saw raw JSON error envelopes.
|
|
191
|
+
*/
|
|
180
192
|
outputError(error, metadata) {
|
|
181
193
|
const errorObj = typeof error === 'string'
|
|
182
194
|
? { message: error }
|
|
183
195
|
: error;
|
|
184
|
-
this.
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
196
|
+
if (this.isJsonMode) {
|
|
197
|
+
this.log(JSON.stringify({
|
|
198
|
+
data: null,
|
|
199
|
+
error: errorObj,
|
|
200
|
+
metadata: {
|
|
201
|
+
timestamp: new Date().toISOString(),
|
|
202
|
+
...metadata,
|
|
203
|
+
},
|
|
204
|
+
}, null, 2));
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
// Pretty mode — mirror the human format used by handleError():
|
|
208
|
+
// <message>
|
|
209
|
+
// 💡 <hint>
|
|
210
|
+
// oclif's error() throws a PrettyPrintableError caught by the framework;
|
|
211
|
+
// it never returns, so callers chained off outputError() (e.g.
|
|
212
|
+
// outputErrorAndExit) reliably terminate.
|
|
213
|
+
const lines = [errorObj.message];
|
|
214
|
+
if (errorObj.hint)
|
|
215
|
+
lines.push(`💡 ${errorObj.hint}`);
|
|
216
|
+
this.error(lines.join('\n'));
|
|
192
217
|
}
|
|
193
218
|
outputErrorAndExit(error, metadata) {
|
|
194
219
|
this.outputError(error, metadata);
|
|
220
|
+
// Only reached in JSON mode — in pretty mode outputError() throws.
|
|
195
221
|
this.exit(1);
|
|
196
222
|
}
|
|
197
223
|
/**
|
|
@@ -387,11 +413,16 @@ export class BaseCommand extends Command {
|
|
|
387
413
|
this.log(` Payload: ${JSON.stringify(payload, null, 2)}`);
|
|
388
414
|
}
|
|
389
415
|
}
|
|
390
|
-
// Handle errors
|
|
416
|
+
// Handle errors. In JSON mode emit a structured envelope for unexpected
|
|
417
|
+
// errors, then re-throw so the framework sets the exit code. Intentional
|
|
418
|
+
// exit errors (oclif ExitError, thrown by this.exit()/outputErrorAndExit)
|
|
419
|
+
// are NOT re-emitted in JSON mode — the command has already produced any
|
|
420
|
+
// envelope it intended to, and re-emitting would double-print a trailing
|
|
421
|
+
// {data:null,error:{EEXIT}} envelope (#34). The framework renders
|
|
422
|
+
// ExitError as empty (no output) and exits non-zero, matching pretty mode.
|
|
391
423
|
async catch(error) {
|
|
392
|
-
if (this.isJsonMode) {
|
|
424
|
+
if (this.isJsonMode && error?.code !== 'EEXIT') {
|
|
393
425
|
this.outputError({ message: error.message, code: error.code || error.constructor.name });
|
|
394
|
-
this.exit(1);
|
|
395
426
|
}
|
|
396
427
|
throw error;
|
|
397
428
|
}
|