howdoi icon indicating copy to clipboard operation
howdoi copied to clipboard

Respect "-j" option on errors

Open adbenitez opened this issue 4 years ago • 3 comments

I am using howdoi programatically, passing -j argument and passing output to json.loads() but on errors, in _format_answers() the error checking has precedence over the "json_output" check, so on errors a normal string is returned instead of a dict that could be parsed and check for "error" key. IMHO if -j argument is used it should be also honored in errors and return a valid JSON instead of a normal message string.

The change is trivial I could open a PR if the idea is accepted.

adbenitez avatar Apr 02 '21 18:04 adbenitez

Thanks for the note. As background, we have an open PR for better logging and also explainability https://github.com/gleitz/howdoi/pull/359

I think it makes sense to have errors also be returned as JSON. I want to make sure that the logging and explainability also play nicely with -j as well.

gleitz avatar Apr 03 '21 16:04 gleitz

Swap these two lines: https://github.com/gleitz/howdoi/blob/master/howdoi/howdoi.py#L468-L472

And then we can back to the old way of checking the sanity_check

assert isinstance(json.loads(howdoi(args).encode('utf-8', 'ignore')), list)

Not sure if the encoding is necessary or not...

gleitz avatar Apr 08 '21 14:04 gleitz

the encode() is not needed I just copy+paste and forgot to remove that, it was used because the previous behavior was comparison with some bytes error message

adbenitez avatar Apr 08 '21 18:04 adbenitez