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

Error handling for missing/wrong API key #89

Closed
Jerry23011 opened this issue Jan 14, 2024 · 9 comments
Closed

Error handling for missing/wrong API key #89

Jerry23011 opened this issue Jan 14, 2024 · 9 comments
Labels
component:swift sdk Issue/PR related to Swift SDK type:bug Something isn't working

Comments

@Jerry23011
Copy link

Description of the bug:

I noticed error handling for unsupportedUserLocation is improved in #85, we also encountered other error types such as API key not valid.

Printing description of error:
 GenerateContentError
   internalError : 1 element
     underlying : RPCError
      - httpResponseCode : 400
      - message : "API key not valid. Please pass a valid API key."
      - status : GoogleGenerativeAI.RPCStatus.invalidArgument

Is it possible to add this case to the package, too?

Actual vs expected behavior:

No response

Any other information you'd like to share?

We're thinking if it is possible to expose RPCError type in Error.swift so that we can call it directly and fetch the error message?

@Jerry23011 Jerry23011 added component:swift sdk Issue/PR related to Swift SDK type:bug Something isn't working labels Jan 14, 2024
@tisfeng
Copy link

tisfeng commented Jan 14, 2024

It is clearly shown here that there is detailed error information, so why is the return type Error instead of RPCError? Why is your RPCError not open to the public?

We prefer to get detailed error information and display it ourselves, instead of the vague error.localizedDescription.

image

@andrewheard
Copy link
Collaborator

Thanks for your feedback, @Jerry23011 and @tisfeng! I agree that this error being buried in GenerateContentError.internalError is difficult to use. I think a few options we could take would be:

  1. Make RPCError public
  2. Introduce new RPC error types, such as FailedPrecondition, similar to the Python SDK
  3. Add an invalidAPIKey case to GenerateContentError, similar to what I proposed in Add unsupportedUserLocation case to GenerateContentError #85

I personally like the last option of a specific case for common errors like this one. I think many developers are likely to run into either invalidAPIKey (copy/paste errors during development, revoked API keys after deployment) and/or unsupportedUserLocation. Making them explicit error cases might also encourage developers to handle them during development (rather than being surprised after deployment when end-users run into them).

Will discuss with my USA-based colleagues tomorrow (Martin Luther King Jr. Day there today). It's late in Europe but @peterfriese might have opinions too.

@tisfeng
Copy link

tisfeng commented Jan 16, 2024

We're glad you're listening to us, and we'd like to make sure that users get clear descriptions of the errors they're getting when they request an error with Gemini, rather than vague, useless information that might otherwise come back and accuse us of having a bad experience with the app (or with Gemini).

We'd love it if you'd make sure to handle all kinds of error types, like this #85, and provide clear descriptive information.

If can't, we hope you'll make the error information publicly available so that we can handle it ourselves.

@andrewheard
Copy link
Collaborator

Thanks, @tisfeng, we definitely appreciate you letting us know about the pain points you're running into -- I'm sure you aren't the only devs with the same questions. Since this issue is similar, I added an example in #82 (comment) of the pain points I ran into when trying to catch a "User location is not supported for the API use." error.

@tisfeng
Copy link

tisfeng commented Jan 17, 2024

Thanks for the reply, I see, seems like using String(describing: error) for now is a workaround, although not so friendly.

Looking forward to the improvements in the later new version.

@tisfeng
Copy link

tisfeng commented Jan 24, 2024

@andrewheard Hi, how is this issue going?

@andrewheard
Copy link
Collaborator

Sorry the delay, @tisfeng. I just published https://github.com/google/generative-ai-swift/releases/tag/0.4.7, which adds an invalidAPIKey case to GenerateContentError. For example usage, please take a look at our Chat Sample or the unit test.

Will close this issue but feel free to re-open if you find this solution doesn't meet your needs.

@Jerry23011
Copy link
Author

Thank you @andrewheard

@elprl
Copy link

elprl commented Aug 17, 2024

I still think this issue was closed prematurely. I'd like to access the RPCError message for the use cases that are not covered in GenerateContentError. For example:

[GoogleGenerativeAI] Response payload: {
  "error": {
    "code": 400,
    "message": "Gemini API free tier is not available in your country. Please enable billing on your project in Google AI Studio.",
    "status": "FAILED_PRECONDITION"
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:swift sdk Issue/PR related to Swift SDK type:bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants