Skip to content

Commit cc757c1

Browse files
E-L-Tclaude
andcommitted
fix(ocr_service): always include code: in failures
OCRService was the only fetch_external_data producer returning a Failure without a code key. Fix it at the source and remove the code-less pattern-match arm from ChampExternalDataConcern. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
1 parent 80e128d commit cc757c1

4 files changed

Lines changed: 28 additions & 8 deletions

File tree

app/models/concerns/champ_external_data_concern.rb

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,6 @@ def handle_result(result)
110110
save_external_error(error, code)
111111
Sentry.capture_exception(error) if code != 404
112112
external_data_error!
113-
in Failure(retryable: false, error:)
114-
save_external_error(error, nil)
115-
Sentry.capture_exception(error)
116-
external_data_error!
117113
end
118114
elsif result.present?
119115
update_external_data!(data: result)

app/services/ocr_service.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,16 +144,17 @@ def self.fetch_ban_address(query)
144144
def self.ocr_url = ENV.fetch("OCR_SERVICE_URL", nil)
145145
def self.document_ia_url = ENV.fetch("DOCUMENT_IA_URL", nil)
146146

147+
# code: is always present: ChampExternalDataConcern#handle_result pattern-matches on it
147148
def self.not_configured(message)
148-
Failure(retryable: false, error: StandardError.new("#{message} not configured"))
149+
Failure(retryable: false, error: StandardError.new("#{message} not configured"), code: nil)
149150
end
150151

151152
def self.to_not_retryable_failure(data)
152153
case data
153154
in code:, error:
154155
Failure(retryable: false, error:, code:)
155156
else
156-
Failure(retryable: false, error: StandardError.new('Unknown error'))
157+
Failure(retryable: false, error: StandardError.new('Unknown error'), code: nil)
157158
end
158159
end
159160
end

spec/models/concerns/champ_external_data_concern_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,12 @@
4848
end
4949
end
5050

51-
context 'with a Failure without code (legacy service — API::Client::Error or un-migrated service)' do
51+
context 'with a non-retryable Failure carrying a nil code (service without HTTP code)' do
5252
it 'persists a nil code exception and moves to external_error' do
5353
allow(champ).to receive(:ready_for_external_call?).and_return(true)
5454
champ.fetch_later!
5555

56-
result = Failure(retryable: false, error: StandardError.new('not configured'))
56+
result = Failure(retryable: false, error: StandardError.new('not configured'), code: nil)
5757
allow(champ).to receive(:fetch_external_data).and_return(result)
5858
champ.fetch!
5959

spec/services/ocr_service_spec.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,22 @@
5454
end
5555
end
5656
end
57+
58+
context 'when the service is not configured' do
59+
let(:blob) { double('Blob', url: 'http://example.com/blob.pdf') }
60+
61+
before do
62+
allow(ENV).to receive(:fetch).and_call_original
63+
allow(ENV).to receive(:fetch).with("OCR_SERVICE_URL", nil).and_return(nil)
64+
end
65+
66+
# ChampExternalDataConcern#handle_result pattern-matches on the code: key
67+
it 'returns a failure carrying a code key' do
68+
analysis = described_class.analyze(blob, nature: 'rib')
69+
expect(analysis.failure?).to be true
70+
expect(analysis.failure).to include(retryable: false, code: nil)
71+
end
72+
end
5773
end
5874

5975
describe '#analyze with unknown nature' do
@@ -65,6 +81,13 @@
6581
end
6682
end
6783

84+
describe '#to_not_retryable_failure with an unrecognized failure shape' do
85+
it 'returns a failure carrying a code key' do
86+
failure = described_class.to_not_retryable_failure(:boom)
87+
expect(failure.failure).to include(retryable: false, code: nil)
88+
end
89+
end
90+
6891
describe '#analyze_2ddoc' do
6992
let(:document_ia_url) { 'https://some_url.fr' }
7093
let(:document_ia_key) { 'some_key' }

0 commit comments

Comments
 (0)