The personal finance app for everyone (by everyone)
Ruby
fix(providers): capture API error response body in PDF processor span output (#2937)
Ellion Blessane69894adb925
diff --git a/app/models/provider/anthropic/concerns/usage_recorder.rb b/app/models/provider/anthropic/concerns/usage_recorder.rbindex ec6eae913b..546440b640 100644--- a/app/models/provider/anthropic/concerns/usage_recorder.rb+++ b/app/models/provider/anthropic/concerns/usage_recorder.rb@@ -74,4 +74,28 @@ module Provider::Anthropic::Concerns::UsageRecorderrescue => e"(message unavailable: #{e.class})"end++ # Anthropic::Errors::APIStatusError carries the parsed response body+ # directly on the error. The body can echo request content (potentially+ # sensitive, since these calls process user-uploaded financial documents),+ # so only allowlisted fields are extracted rather than forwarded whole.+ def safe_error_detail(error)+ body = error.respond_to?(:body) ? error.body : nil+ request_id = error.respond_to?(:headers) ? error.headers&.[]("request-id") : nil+ extract_error_detail(body, request_id)+ rescue => e+ "(detail unavailable: #{e.class})"+ end++ def extract_error_detail(body, request_id)+ body = body.with_indifferent_access if body.respond_to?(:with_indifferent_access)+ nested = body.is_a?(Hash) ? body[:error] : nil+ nested = nested.with_indifferent_access if nested.respond_to?(:with_indifferent_access)++ detail = {}+ detail[:type] = nested[:type] if nested.is_a?(Hash) && nested[:type].present?+ detail[:message] = nested[:message] if nested.is_a?(Hash) && nested[:message].present?+ detail[:request_id] = request_id if request_id.present?+ detail.presence+ endenddiff --git a/app/models/provider/anthropic/pdf_processor.rb b/app/models/provider/anthropic/pdf_processor.rbindex dc6dc2c96a..4b476751b2 100644--- a/app/models/provider/anthropic/pdf_processor.rb+++ b/app/models/provider/anthropic/pdf_processor.rb@@ -53,7 +53,7 @@ class Provider::Anthropic::PdfProcessorspan&.end(output: result.to_h, usage: usage_hash(response.usage))resultrescue => e- span&.end(output: { error: e.message }, level: "ERROR")+ span&.end(output: { error: e.message, error_detail: safe_error_detail(e) }, level: "ERROR")record_usage_error(model, operation: "process_pdf", error: e, metadata: { pdf_size: pdf_content&.bytesize })raiseenddiff --git a/app/models/provider/openai/concerns/usage_recorder.rb b/app/models/provider/openai/concerns/usage_recorder.rbindex ef552dfd1c..b6bb3afd67 100644--- a/app/models/provider/openai/concerns/usage_recorder.rb+++ b/app/models/provider/openai/concerns/usage_recorder.rb@@ -99,4 +99,29 @@ module Provider::Openai::Concerns::UsageRecorderrescue => e"(message unavailable: #{e.class})"end++ # ruby-openai raises Faraday::Error, which carries the parsed response+ # body via response_body. The body can echo request content (potentially+ # sensitive, since these calls process user-uploaded financial documents),+ # so only allowlisted fields are extracted rather than forwarded whole.+ def safe_error_detail(error)+ body = error.respond_to?(:response_body) ? error.response_body : nil+ request_id = error.respond_to?(:response_headers) ? error.response_headers&.[]("x-request-id") : nil+ extract_error_detail(body, request_id)+ rescue => e+ "(detail unavailable: #{e.class})"+ end++ def extract_error_detail(body, request_id)+ body = body.with_indifferent_access if body.respond_to?(:with_indifferent_access)+ nested = body.is_a?(Hash) ? body[:error] : nil+ nested = nested.with_indifferent_access if nested.respond_to?(:with_indifferent_access)++ detail = {}+ detail[:type] = nested[:type] if nested.is_a?(Hash) && nested[:type].present?+ detail[:message] = nested[:message] if nested.is_a?(Hash) && nested[:message].present?+ detail[:code] = nested[:code] if nested.is_a?(Hash) && nested[:code].present?+ detail[:request_id] = request_id if request_id.present?+ detail.presence+ endenddiff --git a/app/models/provider/openai/pdf_processor.rb b/app/models/provider/openai/pdf_processor.rbindex 1cb63e7733..5bf8267b48 100644--- a/app/models/provider/openai/pdf_processor.rb+++ b/app/models/provider/openai/pdf_processor.rb@@ -31,7 +31,7 @@ class Provider::Openai::PdfProcessorspan&.end(output: response.to_h)responserescue => e- span&.end(output: { error: e.message }, level: "ERROR")+ span&.end(output: { error: e.message, error_detail: safe_error_detail(e) }, level: "ERROR")raiseenddiff --git a/test/models/provider/anthropic/pdf_processor_test.rb b/test/models/provider/anthropic/pdf_processor_test.rbindex d2cdbb3a7c..2b807d1795 100644--- a/test/models/provider/anthropic/pdf_processor_test.rb+++ b/test/models/provider/anthropic/pdf_processor_test.rb@@ -99,7 +99,90 @@ class Provider::Anthropic::PdfProcessorTest < ActiveSupport::TestCaseassert_match(/32 MB request limit/i, err.message)end+ test "extracts only allowlisted error fields into span output when the API call fails" do+ error = StandardError.new("boom")+ def error.body+ {+ "type" => "error",+ "error" => { "type" => "invalid_request_error", "message" => "invalid request" },+ "request" => { "document" => "base64-pdf-contents-that-should-never-leak" }+ }+ end++ client = stub_failing_client(error)+ captured_output = nil+ trace = stub_trace { |output| captured_output = output }++ assert_raises(StandardError) do+ Provider::Anthropic::PdfProcessor.new(+ client,+ model: "claude-sonnet-4-6",+ pdf_content: @pdf_content,+ langfuse_trace: trace+ ).process+ end++ assert_equal({ type: "invalid_request_error", message: "invalid request" }, captured_output[:error_detail])+ end++ test "error_detail is nil in span output when the error exposes no body" do+ error = StandardError.new("boom")++ client = stub_failing_client(error)+ captured_output = nil+ trace = stub_trace { |output| captured_output = output }++ assert_raises(StandardError) do+ Provider::Anthropic::PdfProcessor.new(+ client,+ model: "claude-sonnet-4-6",+ pdf_content: @pdf_content,+ langfuse_trace: trace+ ).process+ end++ assert_nil captured_output[:error_detail]+ end++ test "error_detail falls back to a placeholder when reading the body itself raises" do+ error = StandardError.new("boom")+ def error.body+ raise "body accessor exploded"+ end++ client = stub_failing_client(error)+ captured_output = nil+ trace = stub_trace { |output| captured_output = output }++ assert_raises(StandardError) do+ Provider::Anthropic::PdfProcessor.new(+ client,+ model: "claude-sonnet-4-6",+ pdf_content: @pdf_content,+ langfuse_trace: trace+ ).process+ end++ assert_match(/detail unavailable/i, captured_output[:error_detail])+ end+private+ def stub_failing_client(error)+ messages = mock+ messages.expects(:create).raises(error)+ client = mock+ client.stubs(:messages).returns(messages)+ client+ end++ def stub_trace+ span = mock+ span.expects(:end).with { |args| yield(args[:output]); true }+ trace = mock+ trace.stubs(:span).returns(span)+ trace+ end+def stub_client(response)messages = mockmessages.expects(:create).with do |params|diff --git a/test/models/provider/openai/pdf_processor_test.rb b/test/models/provider/openai/pdf_processor_test.rbnew file mode 100644index 0000000000..4f6ff1b2b9--- /dev/null+++ b/test/models/provider/openai/pdf_processor_test.rb@@ -0,0 +1,85 @@+require "test_helper"++class Provider::Openai::PdfProcessorTest < ActiveSupport::TestCase+ setup do+ @pdf_content = "%PDF-1.4 fake bytes".b+ end++ test "extracts only allowlisted error fields into span output when the API call fails" do+ error = StandardError.new("boom")+ def error.response_body+ {+ "error" => { "type" => "invalid_request_error", "message" => "invalid request", "code" => "bad_pdf" },+ "request" => { "messages" => "statement text that should never leak" }+ }+ end+ def error.response_headers+ { "x-request-id" => "req_abc123" }+ end++ captured_output = nil+ trace = stub_trace { |output| captured_output = output }++ assert_raises(StandardError) do+ build_processor(error, trace).process+ end++ assert_equal(+ { type: "invalid_request_error", message: "invalid request", code: "bad_pdf", request_id: "req_abc123" },+ captured_output[:error_detail]+ )+ end++ test "error_detail is nil in span output when the error exposes no response_body" do+ error = StandardError.new("boom")++ captured_output = nil+ trace = stub_trace { |output| captured_output = output }++ assert_raises(StandardError) do+ build_processor(error, trace).process+ end++ assert_nil captured_output[:error_detail]+ end++ test "error_detail falls back to a placeholder when reading response_body itself raises" do+ error = StandardError.new("boom")+ def error.response_body+ raise "response_body accessor exploded"+ end++ captured_output = nil+ trace = stub_trace { |output| captured_output = output }++ assert_raises(StandardError) do+ build_processor(error, trace).process+ end++ assert_match(/detail unavailable/i, captured_output[:error_detail])+ end++ private+ def build_processor(error, trace)+ client = mock+ client.expects(:chat).raises(error)++ processor = Provider::Openai::PdfProcessor.new(+ client,+ model: "gpt-4.1",+ pdf_content: @pdf_content,+ langfuse_trace: trace,+ max_response_tokens: 1000+ )+ processor.stubs(:extract_text_from_pdf).returns("Statement text")+ processor+ end++ def stub_trace+ span = mock+ span.expects(:end).with { |args| yield(args[:output]); true }+ trace = mock+ trace.stubs(:span).returns(span)+ trace+ end+end