Class: ImageServer::Adapters::Http::ErrorHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/image_server/adapters/http/error_handler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ ErrorHandler

Returns a new instance of ErrorHandler.



7
8
9
# File 'lib/image_server/adapters/http/error_handler.rb', line 7

def initialize(http_response)
  @response = http_response
end

Instance Attribute Details

#responseObject (readonly)

Returns the value of attribute response.



5
6
7
# File 'lib/image_server/adapters/http/error_handler.rb', line 5

def response
  @response
end

Instance Method Details

#handle_errors!Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/image_server/adapters/http/error_handler.rb', line 11

def handle_errors!
  case response
    when Net::HTTPOK
    when Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut
      raise ImageServerUnavailable
    when Net::HTTPNotFound
      error = JSON.parse(response.body)['error']
      if error.start_with?('Unable to download image') || error.start_with?('File is empty')
        raise SourceNotFound, error
      elsif error.end_with?('i/o timeout')
        raise Blocked, error
      elsif error.start_with?('ImageMagick failed')
        raise InvalidSource, error
      elsif error.include?('dial tcp')
        raise ConnectionFailure, error
      else
        # generic case, let's log the error so we can add it later
        # but we consider this a permanent error so tht we don't block processing.
        logger.error "error uploading, but error was not recognized: #{error.inspect}"
        raise UploadError, error
      end
    else
      raise UploadError, response
  end
end