valentinegb / openai

An unofficial Rust library for the OpenAI API.
https://crates.io/crates/openai/
MIT License
61 stars 18 forks source link

Handle errors given from API responses #18

Closed valentinegb closed 1 year ago

valentinegb commented 1 year ago

Problem

If the API responds with an error, the program will panic because it fails to deserialize the response, it doesn’t ever expect an error.

Solution

We could deserialize responses into an enum with an error variant and non-error variant. The non-error variant should probably take in a generic which will be the struct expected for a successful request.

Alternatives

We could possibly use the pre-existing Result enum and create a custom error struct, but I’m not sure if we can deserialize into Result.