question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

For HTTP request errors, add response to exception message

See original GitHub issue

Is 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)

cc @SBrandeis @osanseviero @LysandreJik

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:14 (14 by maintainers)

github_iconTop GitHub Comments

3reactions
severocommented, Mar 9, 2022

This loses the full stack trace I think

from e ensures the stack trace is not cut.

https://docs.python.org/3/tutorial/errors.html#exception-chaining

1reaction
coyotte508commented, Mar 14, 2022

The staging backend was updated 😃 (it was long overdue!)

There were a few errors in the curl command, with this it should work:

curl -X DELETE -H "Authorization: Bearer ---token---" -d "{'name': 'repo-does-not-exist'}" https://huggingface.co/api/repos/delete   

But I do get a JSON response 🤔 :

{"error":"Conflict"}

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found