I was running into some problems with the CLI silently failing, so here are some things I found that we need to do to make the CLI error output better:
Minimize the use of try-catch statements
try-catch should only be used when:
We need to recover from the error
We need to add more context to the error before throwing
In most cases we should just let an error happen and let oclif's default error handling take care of it.
Don't use this.exit
Instead of this.exit(1), use this.error('error message here')
Instead of this.exit(0), use return or let control flow fall off the end of the method
Don't use this.logToSterr() or process.stderr.write()
If logging an error, log and exit in the same command with this.error()
If logging a warning, use this.warn()
Add this.debug statements sparingly to a few key places in the code so we can use debug logging when we need to know more about what the command is doing.
See the debug logs by setting the DEBUG=apollo* environment variable, e.g. DEBUG=apollo* yarn dev assembly get
As an example of a couple points above, something like
this.logToStderr(
'Error: End coordinate is lower than the start coordinate',
)
this.exit(1)
should become
this.error('End coordinate is lower than start coordinate')
I was running into some problems with the CLI silently failing, so here are some things I found that we need to do to make the CLI error output better:
try-catch
statementstry-catch
should only be used when:oclif
's default error handling take care of it.this.exit
this.exit(1)
, usethis.error('error message here')
this.exit(0)
, usereturn
or let control flow fall off the end of the methodthis.logToSterr()
orprocess.stderr.write()
this.error()
this.warn()
this.debug
statements sparingly to a few key places in the code so we can use debug logging when we need to know more about what the command is doing.DEBUG=apollo*
environment variable, e.g.DEBUG=apollo* yarn dev assembly get
As an example of a couple points above, something like
should become
And something like this:
should become