@4ge/cli 0.2.2 → 0.2.3
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 +34 -8
- 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
|
/**
|