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

Issue: Improve Error Handling and Provide Detailed Error Messages #82

Closed
ajaymahadeven opened this issue Apr 3, 2024 · 2 comments · Fixed by #112
Closed

Issue: Improve Error Handling and Provide Detailed Error Messages #82

ajaymahadeven opened this issue Apr 3, 2024 · 2 comments · Fixed by #112
Assignees
Labels
component:js sdk Issue/PR related to JavaScript SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working

Comments

@ajaymahadeven
Copy link

Firstly, Thanks for the work on this library :)

Expected Behavior

When an error occurs while using the API, the error message should be structured in a way that makes it easy to extract the reason for the failure. Currently, the error message is all squashed into one, making it tedious to get the reason property from the object, as shown in the following image:

Error Message

Additionally, since the error is of the type any, it makes it difficult to tap into the reason for the failure, as shown in the following image:

Error Type is any

Workaround

To work around this issue, the user has to manually parse the error message to extract the reason for the failure, as shown in the following code snippet:

const makeTheCall = async (
    message: object,
    apikey: string,
    generationConfig: object,
) => {
    await run(message, apikey as string, generationConfig)
        .then((response) => {
            settledState(response as string);
        })
        .catch((error) => {
            if (error.message.includes('[GoogleGenerativeAI Error]')) {
                const errorMessage = error.message;

                if (errorMessage.includes('[400 Bad Request]')) {
                    // Extract the reason for the error
                    const reasonStart = errorMessage.indexOf('reason":"') + 9;
                    const reasonEnd = errorMessage.indexOf('","domain');
                    const reason = errorMessage.substring(
                        reasonStart,
                        reasonEnd,
                    );

                    console.log('Error reason:', reason);
                }
            }
            // errorState(error.reason as string);
            throw new Error(error.message as string);
        });
};

This workaround allows the user to extract the reason for the failure, as shown in the following image:

Error Reason Console logs

Proposed Solution

To improve the error handling and provide more detailed error messages, I suggest the following:

  1. Implement a custom error class that extends the built-in Error class and includes properties for the error code, reason, and any other relevant information.
  2. When an error occurs, create an instance of the custom error class and throw it instead of the raw error object.
  3. Update the error handling logic in the client code to handle the custom error class, making it easier to extract the relevant information from the error.

This approach will provide a more structured and informative error handling experience for the users of the library.

Repo Link

The repository where I encountered this issue is Genie.

@singhniraj08 singhniraj08 added type:bug Something isn't working status:triaged Issue/PR triaged to the corresponding sub-team component:js sdk Issue/PR related to JavaScript SDK labels Apr 4, 2024
@SeoulSKY
Copy link

SeoulSKY commented Apr 18, 2024

I implemented this simple function for a temporary workaround which works for any kind of errors from this library:

function parseStatusCode(error: Error): number {
  const regex = /\[(\d+)[\s\w]*\]/;

  const match = regex.exec(error.message);
  if (match === null) {
    throw new SyntaxError("Couldn't parse the status code from the message: " + error.message);
  }

  return parseInt(match[1]);
}

This function takes the Error object from the library and returns the HTTP status code of the request, which helps you distinguish different kinds of errors.

Example usage:

const genAI = new GoogleGenerativeAI("invalid token");
const model = genAI.getGenerativeModel({model: "gemini-pro"});

try {
  model.generateContent("Hello world!");
} catch (e) {
  const status = parseStatusCode(e);
  if (status === 400) {
    console.error("Invalid API key");
  } else if (status === 500) {
    console.error("Internal Server Error");  
  }
  // other handlers
}

@hsubox76
Copy link
Collaborator

Consolidating additional feedback from another issue (#24):

Related, it doesn't appear that I can import GoogleGenerativeAIError so that I can do e instanceof GoogleGenerativeAIError. Also, guidance on how to differentiate errors would be nice. Currently I don't see a way to get the HTTP status code without parsing the message (e.g. [500 Internal Server Error], [503 Service Unavailable]).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component:js sdk Issue/PR related to JavaScript SDK status:triaged Issue/PR triaged to the corresponding sub-team type:bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants