Module: Mindee::HTTP::Error

Defined in:
lib/mindee/http/error.rb

Overview

Mindee HTTP error module.

Defined Under Namespace

Classes: MindeeHttpClientError, MindeeHttpError, MindeeHttpServerError

Class Method Summary collapse

Class Method Details

.create_error_obj(response) ⇒ Object

Creates an error object based on what's retrieved from a request.

Parameters:

  • response (Hash)

    dictionary response retrieved by the server



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/mindee/http/error.rb', line 25

def create_error_obj(response)
  error_obj = extract_error(response)
  if error_obj.nil?
    error_obj = if response.include?('Maximum pdf pages')
                  {
                    'code' => 'TooManyPages',
                    'message' => 'Maximum amound of pdf pages reached.',
                    'details' => response,
                  }
                elsif response.include?('Max file size is')
                  {
                    'code' => 'FileTooLarge',
                    'message' => 'Maximum file size reached.',
                    'details' => response,
                  }
                elsif response.include?('Invalid file type')
                  {
                    'code' => 'InvalidFiletype',
                    'message' => 'Invalid file type.',
                    'details' => response,
                  }
                elsif response.include?('Gateway timeout')
                  {
                    'code' => 'RequestTimeout',
                    'message' => 'Request timed out.',
                    'details' => response,
                  }
                elsif response.include?('Too Many Requests')
                  {
                    'code' => 'TooManyRequests',
                    'message' => 'Too Many Requests.',
                    'details' => response,
                  }
                else
                  {
                    'code' => 'UnknownError',
                    'message' => 'Server sent back an unexpected reply.',
                    'details' => response,
                  }
                end

  end
  error_obj.nil? ? {} : error_obj
end

.extract_error(response) ⇒ Object

Extracts the HTTP error from the response hash, or the job error if there is one.

Parameters:

  • response (Hash)

    dictionary response retrieved by the server



13
14
15
16
17
18
19
20
21
# File 'lib/mindee/http/error.rb', line 13

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

.handle_error(url, response) ⇒ Object

Creates an appropriate HTTP error exception, based on retrieved http error code

Parameters:

  • url (String)

    the url of the product

  • response (Hash)

    dictionary response retrieved by the server



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/mindee/http/error.rb', line 73

def handle_error(url, response)
  code = response.code.to_i
  begin
    parsed_hash = JSON.parse(response.body, object_class: Hash)
  rescue JSON::ParserError
    parsed_hash = response.body.to_s
  end
  error_obj = create_error_obj(parsed_hash)
  case code
  when 400..499
    MindeeHttpClientError.new(error_obj, url, code)
  when 500..599
    MindeeHttpServerError.new(error_obj, url, code)
  else
    MindeeHttpError.new(error_obj, url, code)
  end
end