Skip to content
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

APIError.Error() doesn't unwrap error correctly #389

Open
leventov opened this issue Dec 12, 2024 · 8 comments
Open

APIError.Error() doesn't unwrap error correctly #389

leventov opened this issue Dec 12, 2024 · 8 comments
Assignees
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@leventov
Copy link

Environment details

  • Package version: 2.14.0

Code: https://github.com/googleapis/gax-go/blob/dc6baf75c1a737233739630b5af6c9759f08abcd/v2/apierror/apierror.go#L202C1-L208C82

Error from POST https://generativelanguage.googleapis.com/v1beta/models/gemini-2.0-flash-exp:streamGenerateContent?%24alt=json%3Benum-encoding%3Dint

httpErr.Message is empty, but httpErr.Body is not empty and contains a JSON list with a single object which has "message" field.

@leventov leventov added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Dec 12, 2024
@quartzmo quartzmo self-assigned this Dec 16, 2024
@quartzmo
Copy link
Member

@leventov Thank you for reporting this issue. Can you provide a log statement with the error, and the JSON? Any more info that you can provide will be helpful, thank you.

@leventov
Copy link
Author

leventov commented Dec 16, 2024

httpErr.Body was exactly this:

[ {
  "error" : {
    "code" : 403,
    "message" : "Method doesn't allow unregistered callers (callers without established identity). Please use API Key or other form of API consumer identity to call this API.",
    "status" : "PERMISSION_DENIED"
  }
} ]

Note slight variation from the shape reported in google-gemini/generative-ai-python#463, where it's an object on the top level with "error" field. But in my case, it is additional array wrapping on top.

@leventov
Copy link
Author

Also FYI, the root cause of the error was that I didn't provide api key but because genai package uses its custom Transport to pass api key. Here's the workaround: https://discuss.ai.google.dev/t/gemini-flash-2-0-error-403-method-doesnt-allow-unregistered-callers/54123/5?u=roman_leventov. This itself is a "usability bug" on the part google's genai Go packages, as I explained in that thread. It's absolutely not clear from the genai package's API that if you provide your custom HTTPClient you bork authentication.

Regardless, I'm not sure the failure the failure of gax to propagate error message was due to that specific "software" nature of the bug.

@quartzmo
Copy link
Member

quartzmo commented Dec 17, 2024

  1. Was the error above produced by https://github.com/google/generative-ai-go?
  2. Can you supply a full repro example, with imports? Please do not omit any code. It is much easier for us to debug the problem with full information (except your actual API key, of course).

@leventov
Copy link
Author

  1. Yes
  2. No, sorry, I moved over and don't want to spend hours on exact reproducible now. Why wouldn't you just add more resilency code to https://github.com/googleapis/gax-go/blob/dc6baf75c1a737233739630b5af6c9759f08abcd/v2/apierror/apierror.go#L202C1-L208C82, checking if httpErr.Message is empty, and if yes, trying to unwrap httpErr.Body in various ways, or even just use it verbatim (still would be better, despite the surrounding clutter, than leaving the user in the dark about the source of error).

@quartzmo
Copy link
Member

Why wouldn't you just...

Did gax-go throw an error, or did it just return an error cleanly but with an empty message?

@leventov
Copy link
Author

leventov commented Dec 19, 2024

It returned error with 403 code but no message. All I'm asking for is for code to try harder to propagate the error up so that debugging is easier. Well, it wasn't also that hard for me, I had to debug the code and set the breakpoint at line 208 here to investigate, but it's not supposed to be required, right? And maybe in some other environments or cross-process proxying settings debugging might me much harder.

Edit: by "no message" I mean that the error message was "googleapi: Error 403: ".

@quartzmo
Copy link
Member

I understand that you are only interested in getting the message. We are also interested in the root cause of the missing message, which is why I need to ask these questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants