Skip to content

Commit

Permalink
🐛 fix invalid error codes (#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianMindee authored Mar 28, 2024
1 parent 4fafe02 commit 0264024
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 8 deletions.
8 changes: 4 additions & 4 deletions lib/mindee/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ def parse_queued(
# * `:on_min_pages` Apply the operation only if document has at least this many pages.
# @param cropper [Boolean, nil] Whether to include cropper results for each page.
# This performs a cropping operation on the server and will increase response time.
# @param initial_delay_sec [Integer, Float, nil] initial delay before polling. Defaults to 4.
# @param delay_sec [Integer, Float, nil] delay between polling attempts. Defaults to 2.
# @param max_retries [Integer, nil] maximum amount of retries. Defaults to 60.
# @param initial_delay_sec [Integer, Float] initial delay before polling. Defaults to 4.
# @param delay_sec [Integer, Float] delay between polling attempts. Defaults to 2.
# @param max_retries [Integer] maximum amount of retries. Defaults to 60.
# @return [Mindee::Parsing::Common::ApiResponse]
def enqueue_and_parse(
input_source,
Expand All @@ -167,7 +167,7 @@ def enqueue_and_parse(
polling_attempts = 1
job_id = enqueue_res.job.id
queue_res = parse_queued(job_id, product_class, endpoint: endpoint)
while (queue_res.job.status != Mindee::Parsing::Common::JobStatus::COMPLETED) && (polling_attempts < max_retries)
while queue_res.job.status != Mindee::Parsing::Common::JobStatus::COMPLETED && polling_attempts < max_retries
sleep(delay_sec)
queue_res = parse_queued(job_id, product_class, endpoint: endpoint)
polling_attempts += 1
Expand Down
14 changes: 13 additions & 1 deletion lib/mindee/http/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,22 @@ module HTTP
module Error
module_function

# Extracts the HTTP error from the response hash, or the job error if there is one.
# @param response [Hash] dictionary response retrieved by the server
def extract_error(response)
return unless response.respond_to?(:each_pair)

if !response.dig('api_request', 'error').empty?
response.dig('api_request', 'error')
elsif !response.dig('job', 'error').empty?
response.dig('job', 'error')
end
end

# Creates an error object based on what's retrieved from a request.
# @param response [Hash] dictionary response retrieved by the server
def create_error_obj(response)
error_obj = response.respond_to?(:each_pair) ? response.dig('api_request', 'error') : nil
error_obj = extract_error(response)
if error_obj.nil?
error_obj = if response.include?('Maximum pdf pages')
{
Expand Down
9 changes: 7 additions & 2 deletions lib/mindee/http/response_validation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ def self.valid_async_response?(response)
return false unless (200..302).cover?(response.code.to_i)

hashed_response = JSON.parse(response.body, object_class: Hash)

return false if hashed_response.dig('job', 'status') == Mindee::Parsing::Common::JobStatus::FAILURE

return false if hashed_response.dig('job', 'error') && !hashed_response.dig('job', 'error').empty?

true
Expand All @@ -41,13 +44,15 @@ def self.valid_async_response?(response)
def self.clean_request!(response)
return response if (response.code.to_i < 200) || (response.code.to_i > 302)

return response unless response.body.empty?
return response if response.body.empty?

hashed_response = JSON.parse(response.body, object_class: Hash)
if hashed_response.dig('api_request', 'status_code').to_i > 399
response.instance_variable_set(:@code, hashed_response['api_request']['status_code'].to_s)
end
return unless hashed_response.dig('job', 'error').empty?

return if !hashed_response.dig('job', 'error').empty? &&
(hashed_response.dig('job', 'status') != Mindee::Parsing::Common::JobStatus::FAILURE.to_s)

response.instance_variable_set(:@code, '500')
end
Expand Down
2 changes: 1 addition & 1 deletion spec/data
18 changes: 18 additions & 0 deletions spec/http/error_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,23 @@
expect(error500.api_message).to eq('Server sent back an unexpected reply.')
expect(error500.api_details).to eq(file.to_s)
end

it 'should fail on a 200 success but job failed' do
file = File.read("#{DATA_DIR}/async/get_failed_job_error.json")
error_obj = MockHTTPResponse.new('1.0', '200', 'success', file)
hashed_obj = JSON.parse(error_obj.body)
expect(error_obj.code.to_i).to eq(200)
expect(hashed_obj.dig('job', 'status')).to eq('failed')
expect(Mindee::HTTP::ResponseValidation.valid_async_response?(error_obj)).to be(false)
Mindee::HTTP::ResponseValidation.clean_request! error_obj
error500 = Mindee::HTTP::Error.handle_error('dummy-url', error_obj)
expect do
raise error500
end.to raise_error Mindee::HTTP::Error::MindeeHttpServerError
expect(error500.status_code).to eq(500)
expect(error500.api_code).to eq('ServerError')
expect(error500.api_message).to eq('An error occurred')
expect(error500.api_details).to eq('An error occurred')
end
end
end

0 comments on commit 0264024

Please sign in to comment.