bktide
Version:
Command-line interface for Buildkite CI/CD workflows with rich shell completions (Fish, Bash, Zsh) and Alfred workflow integration for macOS power users
63 lines • 2.02 kB
JavaScript
import { BaseErrorFormatter } from './Formatter.js';
/**
* JSON formatter for errors
*/
export class JsonFormatter extends BaseErrorFormatter {
name = 'json';
/**
* Format one or more errors for display in JSON format
*
* @param errors The error(s) to format
* @param options Formatting options
* @returns JSON formatted error message
*/
formatError(errors, options) {
const errorArray = Array.isArray(errors) ? errors : [errors];
const formattedErrors = errorArray.map(error => this.formatSingleError(error, options?.debug));
const result = {
success: false,
errors: formattedErrors,
debug: options?.debug ? this.getDebugInfo() : undefined
};
return JSON.stringify(result, null, 2);
}
/**
* Format a single error object
* @param error The error to format
* @param includeDebug Whether to include debug information
* @returns Formatted error object
*/
formatSingleError(error, includeDebug = false) {
const result = {
name: this.getErrorName(error),
message: this.getErrorMessage(error)
};
if (includeDebug) {
const stack = this.getStackTrace(error);
if (stack) {
result.stack = stack;
}
}
const apiErrors = this.getApiErrors(error);
if (apiErrors?.length) {
result.apiErrors = apiErrors;
}
const request = this.getRequestDetails(error);
if (request && includeDebug) {
result.request = request;
}
return result;
}
/**
* Get debug information about the system
* @returns Debug information object
*/
getDebugInfo() {
return {
timestamp: new Date().toISOString(),
nodeVersion: process.version,
platform: `${process.platform} (${process.arch})`
};
}
}
//# sourceMappingURL=JsonFormatter.js.map