adamrushy / OpenAISwift

This is a wrapper library around the ChatGPT and OpenAI HTTP API
MIT License
1.6k stars 242 forks source link

Feature/throw bad responses #60

Open ConfusedVorlon opened 1 year ago

ConfusedVorlon commented 1 year ago

Currently, "error responses" from OpenAI (such as when a bad API token is used) are not treated as errors. This means that the response is fed through to the parser, and fails to decode.

The coder sees a decoding error, rather than the actual problem.

By throwing earlier - we can give a more helpful response in the console.

e.g. RequestFailed: genericError(error: OpenAISwift.OpenAIError.badResponse(code: 401, response: Optional("{\n \"error\": {\n \"message\": \"Incorrect API key provided: ##YOUR C******************EY##. You can find your API key at https://platform.openai.com/account/api-keys.\",\n \"type\": \"invalid_request_error\",\n \"param\": null,\n \"code\": \"invalid_api_key\"\n }\n}\n")))