openai-cpp icon indicating copy to clipboard operation
openai-cpp copied to clipboard

Better error handling when not using exceptions

Open dgm3333 opened this issue 2 years ago • 1 comments

Using VS 2022 The following response from OpenAI triggers a fatal but silent throw. Errors would be better handled by setting an error flag and storing the output for user handling or allowing user-definition of an error handling callback (or at the very least reporting the issue before crashing).

Response is:
{
  "error": {
    "code": null,
    "message": "You exceeded your current quota, please check your plan and billing details.",
    "param": null,
    "type": "insufficient_quota"
  }
}
    void trigger_error(const std::string& msg) {
//        if (throw_exception_) {
//            throw std::runtime_error(msg);
//        }
//        else {
            std::cerr << "[OpenAI] error. Reason: " << msg << '\n';
//        }
    }

dgm3333 avatar Jun 25 '23 12:06 dgm3333

It is recommended to handle exceptions

As referenced in https://github.com/olrea/openai-cpp#a-word-about-error-handling and https://github.com/olrea/openai-cpp/blob/main/examples/09-instances.cpp, OpenAI CPP makes use of exceptions, you should use try and catch in your code in order to handle the errors appropriately.

If you do not wish to use exceptions

If you do not want to use exceptions you can just set

instance.setThrowException(false);

That said, you are right it should be nice when we do not use exceptions to have a better error handling systems as you proposed. I will put flag as enhancement for this. Feel free to participate :)

coin-au-carre avatar Jul 03 '23 18:07 coin-au-carre