Method: Docker::Connection#request

Defined in:
lib/docker/connection.rb

#request(*args, &block) ⇒ Object

Send a request to the server with the ‘



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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/docker/connection.rb', line 40

def request(*args, &block)
  retries ||= 0
  request = compile_request_params(*args, &block)
  log_request(request)
  begin
    resource.request(request).body
  rescue Excon::Errors::BadRequest => ex
    if retries < 2
      response_cause = ''
      begin
        response_cause = JSON.parse(ex.response.body)['cause']
      rescue JSON::ParserError
        #noop
      end

      if response_cause.is_a?(String)
        # The error message will tell the application type given and then the
        # application type that the message should be
        #
        # This is not perfect since it relies on processing a message that
        # could change in the future. However, it should be a good stop-gap
        # until all methods are updated to pass in the appropriate content
        # type.
        #
        # A current example message is:
        #   * 'Content-Type: application/json is not supported. Should be "application/x-tar"'
        matches = response_cause.delete('"\'').scan(%r{(application/\S+)})
        unless matches.count < 2
          Docker.logger.warn(
            <<~RETRY_WARNING
            Automatically retrying with content type '#{response_cause}'
              Original Error: #{ex}
            RETRY_WARNING
          ) if Docker.logger

          request[:headers]['Content-Type'] = matches.last.first
          retries += 1
          retry
        end
      end
    end
    raise ClientError, ex.response.body
  rescue Excon::Errors::Unauthorized => ex
    raise UnauthorizedError, ex.response.body
  rescue Excon::Errors::NotFound => ex
    raise NotFoundError, ex.response.body
  rescue Excon::Errors::Conflict => ex
    raise ConflictError, ex.response.body
  rescue Excon::Errors::InternalServerError => ex
    raise ServerError, ex.response.body
  rescue Excon::Errors::Timeout => ex
    raise TimeoutError, ex.message
  end
end