For HTTP request errors, add response to exception message
See original GitHub issueIs your feature request related to a problem? Please describe.
Most of the time, hf.co sends a JSON response along with HTTP error codes. But it’s not shown in the stack trace:
requests.exceptions.HTTPError: 404 Client Error: Not Found for url: http://localhost:5564/api/repos/delete
Which leads hf.co to send a 409 here even though it’s really not the correct HTTP code (see https://github.com/huggingface/moon-landing/issues/2272)
Describe the solution you’d like Add the error message from hf.co when available.
I tried this in HfApi.delete_repo
:
try:
r.raise_for_status()
except requests.exceptions.RequestException as e:
message = e.response.json()["error"]
import sys
raise type(e)(str(e) + " - " + message).with_traceback(sys.exc_info()[2])
This seems to work, but I suck very much at Python and it’s a handful of lines. (and would cause an unrelated error when response.json()
fails)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:14 (14 by maintainers)
Top Results From Across the Web
Custom Error Message Handling for REST API - Baeldung
In this tutorial, we'll discuss how to implement a global error handler for a Spring REST API. We will use the semantics of...
Read more >Handling Exceptions Returned from the Web API
You'll learn to determine what kind of error is returned from your Web API call and display different messages based on those errors....
Read more >Exception Handling in ASP.NET Web API - Microsoft Learn
The HttpResponseException type is a special case. This exception returns any HTTP status code that you specify in the exception constructor.
Read more >HTTP response status codes - MDN Web Docs - Mozilla
HTTP response status codes indicate whether a specific HTTP ... be a client error (e.g., malformed request syntax, invalid request message ...
Read more >Custom Error Messages in Spring REST API - amitph
The Exception handler class has three exception handlers, each of which returns a specific HTTP Response Status. Each response status specifies a reason...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
from e
ensures the stack trace is not cut.https://docs.python.org/3/tutorial/errors.html#exception-chaining
The staging backend was updated 😃 (it was long overdue!)
There were a few errors in the curl command, with this it should work:
But I do get a JSON response 🤔 :
Maybe if the message isn’t JSON just send the original error? (or the plain text message)
And yes it would be fine, thank you 😃 We will update the message with the status code on our side afterwards.