Exception: Docker::API::Error

Inherits:
StandardError
  • Object
show all
Defined in:
lib/docker/api/errors.rb,
sig/docker/api/core.rbs

Overview

The root of every error this gem raises.

Nothing beneath the abstraction escapes: no Errno, no OpenSSL exception, no Net:: class and no JSON::ParserError reaches a caller's rescue clause. The original is always retained as #cause, so nothing is lost -- but consumers get to rescue one hierarchy instead of coupling themselves to whichever HTTP library happens to be underneath.

Examples:

Rescuing anything this gem can raise

begin
  client.containers.get("missing")
rescue Docker::API::Error => e
  warn "#{e.operation} failed: #{e.message}"
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(message = nil, operation: nil, status: nil, response: nil) ⇒ Error

Returns a new instance of Error.

Parameters:

  • message (String, nil) (defaults to: nil)

    a message, or nil to compose one

  • operation (String, Symbol, nil) (defaults to: nil)

    the operation that failed

  • status (Integer, nil) (defaults to: nil)

    the HTTP status

  • response (Docker::API::Response, nil) (defaults to: nil)

    the response

  • operation: (String, Symbol, nil) (defaults to: nil)
  • status: (Integer, nil) (defaults to: nil)
  • response: (Docker::API::Response, nil) (defaults to: nil)


36
37
38
39
40
41
# File 'lib/docker/api/errors.rb', line 36

def initialize(message = nil, operation: nil, status: nil, response: nil)
  @operation = operation&.to_s
  @status = status
  @response = response
  super(message || compose_message)
end

Instance Attribute Details

#operationString? (readonly)

Returns the operation that failed, e.g. "container_start".

Returns:

  • (String, nil)

    the operation that failed, e.g. "container_start"



24
25
26
# File 'lib/docker/api/errors.rb', line 24

def operation
  @operation
end

#responseDocker::API::Response? (readonly)

Returns the response, when there was one.

Returns:



30
31
32
# File 'lib/docker/api/errors.rb', line 30

def response
  @response
end

#statusInteger? (readonly)

Returns the HTTP status the daemon returned.

Returns:

  • (Integer, nil)

    the HTTP status the daemon returned



27
28
29
# File 'lib/docker/api/errors.rb', line 27

def status
  @status
end

Class Method Details

.for(status:, operation: nil, response: nil) ⇒ Docker::API::Error

Build the error class appropriate to an HTTP status.

Parameters:

  • status (Integer)

    the HTTP status the daemon returned

  • operation (String, Symbol, nil) (defaults to: nil)

    the operation that failed

  • response (Docker::API::Response, nil) (defaults to: nil)

    the response

  • status: (Integer)
  • operation: (String, Symbol, nil) (defaults to: nil)
  • response: (Docker::API::Response, nil) (defaults to: nil)

Returns:



49
50
51
# File 'lib/docker/api/errors.rb', line 49

def self.for(status:, operation: nil, response: nil)
  klass_for(status).new(nil, operation: operation, status: status, response: response)
end