Skip to content

Commit

Permalink
fix(cli): map all Errors to GardenErrors and log accordingly
Browse files Browse the repository at this point in the history
  • Loading branch information
eysi09 committed May 27, 2018
1 parent 95368cc commit 02b05b3
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 14 deletions.
20 changes: 12 additions & 8 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import {
GardenError,
InternalError,
PluginError,
toGardenError,
} from "./exceptions"
import { Garden } from "./garden"
import { FileWriter } from "./logger/writers/file-writer"
Expand Down Expand Up @@ -346,37 +347,40 @@ export class GardenCli {

async parse(): Promise<ParseResults> {
const parseResult: SywacParseResults = await this.program.parse()
const { argv, details, errors: parseErrors, output: cliOutput } = parseResult
const { argv, details, errors, output: cliOutput } = parseResult
const commandResult = details.result
const { output } = argv

let code = parseResult.code

// --help or --version options were called so we log the cli output and exit
if (cliOutput && parseErrors.length < 1) {
if (cliOutput && errors.length < 1) {
this.logger.stop()
console.log(cliOutput)
process.exit(parseResult.code)
}

const errors: GardenError[] = parseErrors
.map(e => ({ type: "parameter", message: e.toString() }))
const gardenErrors: GardenError[] = errors
.map(toGardenError)
.concat((commandResult && commandResult.errors) || [])

// --output option set
if (output) {
const renderer = OUTPUT_RENDERERS[output]
if (errors.length > 0) {
console.error(renderer({ success: false, errors }))
if (gardenErrors.length > 0) {
console.error(renderer({ success: false, errors: gardenErrors }))
} else {
console.log(renderer({ success: true, ...commandResult }))
}
// Note: this circumvents an issue where the process exits before the output is fully flushed
await sleep(100)
}

if (errors.length > 0) {
errors.forEach(err => this.logger.error({ msg: err.message, error: err }))
if (gardenErrors.length > 0) {
gardenErrors.forEach(error => this.logger.error({
msg: error.message,
error,
}))

if (this.logger.writers.find(w => w instanceof FileWriter)) {
this.logger.info(`\nSee ${ERROR_LOG_FILENAME} for detailed error message`)
Expand Down
2 changes: 1 addition & 1 deletion src/exceptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export abstract class GardenBaseError extends Error implements GardenError {
}
}

export function toGardenError(err: Error): GardenError {
export function toGardenError(err: Error | GardenError): GardenError {
if (err instanceof GardenBaseError) {
return err
} else {
Expand Down
8 changes: 4 additions & 4 deletions src/logger/renderers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,17 @@ export function renderEmoji(entry: LogEntry): string {
}

export function renderError(entry: LogEntry) {
const { error } = entry.opts
const { msg, error } = entry.opts
if (error) {
let out = `${error.stack || error.message}`
const detail = (<any>error).detail
const { detail, message, stack } = error
let out = `${stack || message}`
if (detail) {
const yamlDetail = yaml.safeDump(detail, { noRefs: true, skipInvalid: true })
out += `\nError Details:\n${yamlDetail}`
}
return out
}
return ""
return msg || ""
}

export function renderSymbol(entry: LogEntry): string {
Expand Down
2 changes: 1 addition & 1 deletion src/logger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export interface LogEntryOpts {
append?: boolean
fromStdStream?: boolean
showDuration?: boolean
error?: GardenError | Error
error?: GardenError
id?: string
unindentChildren?: boolean
}

0 comments on commit 02b05b3

Please sign in to comment.