@@ -163,11 +163,12 @@ def stub_chat
163163 end
164164 end
165165
166- shared_examples "safe provider diagnostics" do |operation , prefix |
166+ shared_examples "bounded provider diagnostics" do |operation , prefix |
167167 let ( :mock_client ) { instance_double ( OpenAI ::Client ) }
168168 let ( :detail ) do
169169 { "error" => { "type" => "invalid_request_error" , "code" => "context_length_exceeded" ,
170- "message" => "Request contained private-plan-content and sk-secret-key" } }
170+ "message" => "This model supports at most 128000 tokens." ,
171+ "request" => { "content" => "private-plan-content" , "api_key" => "sk-secret-key" } } }
171172 end
172173 let ( :body ) { detail }
173174 let ( :failure ) { Faraday ::BadRequestError . new ( "the server responded with status 400" , { status : 400 , body : body } ) }
@@ -193,51 +194,77 @@ def request
193194
194195 let ( :operation ) { operation }
195196
196- it "keeps recognized reasons without echoing the provider message " do
197+ it "keeps the provider explanation without appending other response fields " do
197198 expect { request } . to raise_error ( described_class ::Error ,
198- "#{ prefix } : the server responded with status 400 (invalid_request_error/context_length_exceeded)" ) { |error |
199+ "#{ prefix } : the server responded with status 400 (invalid_request_error/context_length_exceeded: This model supports at most 128000 tokens. )" ) { |error |
199200 expect ( error . cause ) . to equal ( failure )
200201 }
201202 end
202203
203204 context "with a JSON body" do
204205 let ( :body ) { detail . to_json }
205206
206- it "keeps the same safe identifiers " do
207+ it "keeps the same provider explanation " do
207208 expect { request } . to raise_error ( described_class ::Error ,
208- "#{ prefix } : the server responded with status 400 (invalid_request_error/context_length_exceeded)" )
209+ "#{ prefix } : the server responded with status 400 (invalid_request_error/context_length_exceeded: This model supports at most 128000 tokens. )" )
209210 end
210211 end
211212
212213 [ nil , "" , "<html>Bad gateway</html>" , "{broken" , "null" , "[]" , '"error"' , [ ] ,
213- { "error" => "private-plan-content sk-secret-key" } , { "error" => nil } , { "error" => [ ] } ,
214- { "error" => { } } , { "error" => { "message" => { "prompt" => "private-plan-content" } } } ,
215- { "error" => { "type" => "sk-secret-key" , "code" => "private-plan-content" } } ,
216- { "error" => { "type" => "a" * 10_000 , "code" => "b" * 10_000 } } ,
214+ { "error" => nil } , { "error" => [ ] } , { "error" => { } } , { "error" => " \n " } ,
215+ { "error" => { "message" => { "prompt" => "private-plan-content" } } } ,
217216 { "error" => { "type" => [ "invalid_request_error" ] , "code" => { "secret" => "value" } } } ,
218- { "error" => { "type" => "invalid_request_error \n forged log line" , "code " => false } }
217+ { "error" => { "type" => true , "code" => false , "message " => 123 } }
219218 ] . each_with_index do |malformed_body , index |
220- context "with an untrusted or unusable error body (#{ index } )" do
219+ context "with an unusable error body (#{ index } )" do
221220 let ( :body ) { malformed_body }
222221
223- it "falls back to the original status without leaking text or masking the failure" do
222+ it "falls back to the original status without masking the failure" do
224223 expect { request } . to raise_error ( described_class ::Error , "#{ prefix } : the server responded with status 400" ) { |error |
225224 expect ( error . cause ) . to equal ( failure )
226225 }
227226 end
228227 end
229228 end
230229
231- it "retains a recognized type when the code is unknown " do
232- detail [ "error" ] [ " code"] = "sk-secret-key"
230+ it "keeps gateway-specific error types, codes and messages " do
231+ detail [ "error" ] = { "type" => "gateway_request_error" , " code" => "custom_limit" , "message" => "Try a smaller request." }
233232 expect { request } . to raise_error ( described_class ::Error ,
234- "#{ prefix } : the server responded with status 400 (invalid_request_error )" )
233+ "#{ prefix } : the server responded with status 400 (gateway_request_error/custom_limit: Try a smaller request. )" )
235234 end
236235
237- it "retains a recognized code when the type is unknown" do
238- detail [ "error" ] [ "type" ] = "private-plan-content"
236+ it "keeps a gateway's plain-string error" do
237+ detail [ "error" ] = "prompt too long"
238+ expect { request } . to raise_error ( described_class ::Error , "#{ prefix } : the server responded with status 400 (prompt too long)" )
239+ end
240+
241+ it "normalizes whitespace and control characters in every field" do
242+ detail [ "error" ] = { "type" => " gateway\n error " , "code" => " custom\t limit " , "message" => " Try\u0000 again. " }
243+ expect { request } . to raise_error ( described_class ::Error ,
244+ "#{ prefix } : the server responded with status 400 (gateway error/custom limit: Try again.)" )
245+ end
246+
247+ it "handles invalid string encoding in decoded gateway bodies" do
248+ detail [ "error" ] = { "message" => "bad \xFF response" . force_encoding ( "UTF-8" ) }
239249 expect { request } . to raise_error ( described_class ::Error ,
240- "#{ prefix } : the server responded with status 400 (context_length_exceeded)" )
250+ "#{ prefix } : the server responded with status 400 (bad \uFFFD response)" )
251+ end
252+
253+ %w[ type code message ] . each do |field |
254+ it "bounds an oversized #{ field } " do
255+ detail [ "error" ] = { field => "x" * 10_000 }
256+ expect { request } . to raise_error ( described_class ::Error ,
257+ "#{ prefix } : the server responded with status 400 (#{ 'x' * 297 } ...)" )
258+ end
259+ end
260+
261+ it "bounds the combined diagnostic" do
262+ detail [ "error" ] = { "type" => "t" * 100 , "code" => "c" * 100 , "message" => "m" * 100 }
263+ expect { request } . to raise_error ( described_class ::Error ) { |error |
264+ suffix = error . message . delete_prefix ( "#{ prefix } : the server responded with status 400" )
265+ expect ( suffix . length ) . to eq ( 303 )
266+ expect ( suffix ) . to end_with ( "...)" )
267+ }
241268 end
242269
243270 context "when Faraday cannot parse the gateway response" do
@@ -271,10 +298,10 @@ def request
271298 end
272299
273300 describe "chat errors" do
274- include_examples "safe provider diagnostics" , :chat , "Chat request failed"
301+ include_examples "bounded provider diagnostics" , :chat , "Chat request failed"
275302 end
276303
277304 describe "transcription errors" do
278- include_examples "safe provider diagnostics" , :transcribe , "Transcription failed"
305+ include_examples "bounded provider diagnostics" , :transcribe , "Transcription failed"
279306 end
280307end
0 commit comments