diff --git a/lib/chatgpt/client.rb b/lib/chatgpt/client.rb index 81ebb3c..1378d3e 100644 --- a/lib/chatgpt/client.rb +++ b/lib/chatgpt/client.rb @@ -99,8 +99,9 @@ def request_api(url, data, method = :post) # Parse and return the response body JSON.parse(response.body) rescue RestClient::ExceptionWithResponse => e - # Parse the error message from the API response - error_msg = JSON.parse(e.response.body)['error']['message'] + error_msg = 'No error message' + # Parse the error message from the API response if there is a response + error_msg = JSON.parse(e.response.body)['error']['message'] if e.response # Raise an exception with the API error message raise RestClient::ExceptionWithResponse.new("#{e.message}: #{error_msg} (#{e.http_code})"), nil, e.backtrace