Skip to content

Commit

Permalink
Merge pull request #3 from ujh/fix-error-handling-when-there-is-no-re…
Browse files Browse the repository at this point in the history
…sponse

Fix error handling when there is no response
  • Loading branch information
nagstler authored Oct 8, 2024
2 parents fdc75ac + a65c382 commit cd4a3e8
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/chatgpt/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit cd4a3e8

Please sign in to comment.