howdoi
howdoi copied to clipboard
Respect "-j" option on errors
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.
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.
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...
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