-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
.destfile
unexpectedly written even on error
#178
Comments
What version of gh did you use? |
I get the same behaviour on both 1.4.0 from CRAN and the latest git HEAD. The relevant code I think is:
It writes the request to the output file first and only checks the response status afterwards. |
I went further back in time, i.e. to gh v1.3.1 and v1.3.0, and the behaviour was the same ( |
I guess this is a side effect of how HTTP works. Even if the status code is an error, the response has a body, and the HTTP client that handles So I guess we should delete it on error? That can also lead some weird consequences, e.g. if you want to overwrite a file, and there is an error, then it will be deleted. |
We could write the response to a temporary file and then rename it on success and delete it on failure. |
When
.destfile
is provided,gh
will write to it even if the API returned an error:In the absence of
.destfile
, when the request succeeds (e.g. changeDOES_NOT_EXIST
toREADME.md
), the contents of the file are printed out. When it fails there's an error message, but nothing else is printed. However when.destfile
is given, that file is written to whether or not the request succeeded. In the error case, the JSON-encoded error is put in the file even when.accept
specified that we were expecting to write something other than JSON.It's a bit of a surprising behaviour, and it's the underlying cause of a bug in
usethis
(r-lib/usethis#1774). Granted,usethis
could have been handling the error more carefully.I think it would be less surprising if the
.destfile
were only created and written when the API request succeeded. All data from the API error already appears in the R error, so we don't lose any information.The text was updated successfully, but these errors were encountered: