pkgx icon indicating copy to clipboard operation
pkgx copied to clipboard

Handle error messages for unknown args better

Open secondary-smiles opened this issue 3 years ago • 13 comments

secondary-smiles avatar Dec 03 '22 23:12 secondary-smiles

  • Added a try/catch block to the app.ts file
  • Changed some of the error messages in useFlags() and added an internalSetVerbosity function that is called when there's an error with flags
  • Made changes to TeaError class, including adding new cases for not-found: flag errors (which are thrown by useArgs()) and changing how panic works so it throws a normal Error instead of a TeaError object

what-the-diff[bot] avatar Dec 03 '22 23:12 what-the-diff[bot]

I'm going to be honest, I'm confused as to how this helps.

lino-levan avatar Dec 04 '22 17:12 lino-levan

Previously, if tea was passed a flag that it didn't recognize it would throw an error which wasn't consistent with most of the other tea errors. Also, if it were passed '+', '-', or '--' without any flags or commands it would try to continue which can lead to unrelated errors. This PR just adds a little bit more checking while parsing flags as well as a better panic handler that produces a cleaner output.

secondary-smiles avatar Dec 04 '22 17:12 secondary-smiles

That makes sense. I guess my question more specifically is:

why is

 if (message) {
   console.error(`panic: ${message}`)
 }
 Deno.exit(1)

better than

throw new Error(message)

I'm of the camp that believes we should use Deno.exit sparingly. Is throwing an error not good enough? Are we using this to get around try {} catch {} blocks? I'm curious to hear the reasoning here.

lino-levan avatar Dec 04 '22 18:12 lino-levan

When Deno throws an error it leaves a trace.

error: Uncaught Error: unknown flag: -g
                        throw new Error(`unknown flag: -${c}`);
                              ^
    at useArgs (file:///opt/tea.xyz/src/v0.16.0/src/hooks/useFlags.ts:183:31)
    at file:///opt/tea.xyz/src/v0.16.0/src/app.ts:12:27

While I agree that Deno.exit is a last resort method, I think it's worth using to get rid of the trace in production. I don't know if Deno provides an option to hook Error throws but if there is then that would probably be a better thing to do. I couldn't find anything like that in the docs though.

secondary-smiles avatar Dec 04 '22 19:12 secondary-smiles

While I agree that Deno.exit is a last resort method, I think it's worth using to get rid of the trace in production. I don't know if Deno provides an option to hook Error throws but if there is then that would probably be a better thing to do. I couldn't find anything like that in the docs though.

Perhaps the middle ground is to handle this based on Verbosity. We could throw with the stack trace at a higher Verbosity, and exit more cleanly with just a message and an exit code at lower levels.

It might also be useful to provide the tea-specific error code to exit, for more useful constructs.

jhheider avatar Dec 04 '22 19:12 jhheider

Agree with @jhheider on this one. Verbosity should play a role in this 100%.

lino-levan avatar Dec 04 '22 19:12 lino-levan

I also agree. I'll revert the panic() usage and function and see about making a new PR to address it.

secondary-smiles avatar Dec 04 '22 19:12 secondary-smiles

I think you could probably just throw in a

const { verbosity } = useFlags()

switch (verbosity) {

and customize behavior from there.

jhheider avatar Dec 04 '22 20:12 jhheider

I think @secondary-smiles could probably just do this in this PR. @jhheider's example implementation of this LGTM.

lino-levan avatar Dec 04 '22 20:12 lino-levan

Sure thing, I'll get to it in a bit.

secondary-smiles avatar Dec 05 '22 01:12 secondary-smiles

This is a welcome addition.

We create TeaError for errors that are known and acceptable and should not have a stack trace. I believe panic() should continue to show a trace in all situations since we use it to mean “if this happens it is programmer error”.

So create a new TeaError category?

mxcl avatar Dec 06 '22 16:12 mxcl

Sure. I haven't gotten super familiar with the sourcecode yet, but I'll take a look later and work on a TeaError for arg parsing.

secondary-smiles avatar Dec 06 '22 20:12 secondary-smiles

Thanks for the contribution 👏🏻

mxcl avatar Dec 12 '22 21:12 mxcl