Skip to content

Commit 5f68b6e

Browse files
committed
Add default error logging
1 parent d4dd850 commit 5f68b6e

File tree

5 files changed

+20
-3
lines changed

5 files changed

+20
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Added
1111

12-
- Add ability to pass [Faraday middleware](https://lostisland.github.io/faraday/#/middleware/index) to the client in a block, eg. to enable verbose logging - thanks to [@atesgoral](
12+
- Add ability to pass [Faraday middleware](https://lostisland.github.io/faraday/#/middleware/index) to the client in a block, eg. to enable verbose logging - shout out to [@obie](https://github.com/obie) for pushing for this.
1313
- Add better error logging to the client by default.
14-
- Bump Event Source to v1, thank you [@atesgoral](https://github.com/atesgoral) @ Shopify for this!
14+
- Bump Event Source to v1, thank you [@atesgoral](https://github.com/atesgoral) @ Shopify!
1515

1616
## [6.2.0] - 2023-11-15
1717

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ end
110110

111111
#### Verbose Logging
112112

113-
You can pass [Faraday middleware](https://lostisland.github.io/faraday/#/middleware/index) to the client in a block, eg. to enable verbose logging:
113+
You can pass [Faraday middleware](https://lostisland.github.io/faraday/#/middleware/index) to the client in a block, eg. to enable verbose logging with Ruby's [Logger](https://ruby-doc.org/3.2.2/stdlibs/logger/Logger.html):
114114

115115
```ruby
116116
client = OpenAI::Client.new do |f|

lib/openai.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,21 @@ module OpenAI
1919
class Error < StandardError; end
2020
class ConfigurationError < Error; end
2121

22+
class MiddlewareErrors < Faraday::Middleware
23+
def call(env)
24+
begin
25+
@app.call(env)
26+
rescue Faraday::Error => e
27+
raise e unless e.response.is_a?(Hash)
28+
29+
logger = Logger.new(STDOUT)
30+
logger.error(e.response[:body])
31+
32+
raise e
33+
end
34+
end
35+
end
36+
2237
class Configuration
2338
attr_writer :access_token
2439
attr_accessor :api_type, :api_version, :organization_id, :uri_base, :request_timeout,

lib/openai/compatibility.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ module OpenAI
55
Error = ::OpenAI::Error
66
ConfigurationError = ::OpenAI::ConfigurationError
77
Configuration = ::OpenAI::Configuration
8+
MiddlewareErrors = ::OpenAI::MiddlewareErrors
89
end
910
end

lib/openai/http.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ def conn(multipart: false)
7474
connection = Faraday.new do |f|
7575
f.options[:timeout] = @request_timeout
7676
f.request(:multipart) if multipart
77+
f.use MiddlewareErrors
7778
f.response :raise_error
7879
f.response :json
7980
end

0 commit comments

Comments
 (0)