Exception: Aws::AwsError

Inherits:
RuntimeError
  • Object
show all
Defined in:
lib/awsbase/errors.rb

Overview

Exception class to signal any Amazon errors. All errors occuring during calls to Amazon’s web services raise this type of error. Attribute inherited by RuntimeError:

message    - the text of the error, generally as returned by AWS in its XML response.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors = nil, http_code = nil, request_id = nil, request_data = nil, response = nil) ⇒ AwsError

Returns a new instance of AwsError.



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/awsbase/errors.rb', line 28

def initialize(errors=nil, http_code=nil, request_id=nil, request_data=nil, response=nil)
  @errors       = errors
  @request_id   = request_id
  @http_code    = http_code
  @request_data = request_data
  @response     = response
  msg           = @errors.is_a?(Array) ? @errors.map { |code, msg| "#{code}: #{msg}" }.join("; ") : @errors.to_s
  msg += "\nREQUEST=#{@request_data} " unless @request_data.nil?
  msg += "\nREQUEST ID=#{@request_id} " unless @request_id.nil?
  super(msg)
end

Instance Attribute Details

#errorsObject (readonly)

either an array of errors where each item is itself an array of [code, message]), or an error string if the error was raised manually, as in AwsError.new('err_text')



15
16
17
# File 'lib/awsbase/errors.rb', line 15

def errors
  @errors
end

#http_codeObject (readonly)

Response HTTP error code



21
22
23
# File 'lib/awsbase/errors.rb', line 21

def http_code
  @http_code
end

#request_dataObject (readonly)

Raw request text data to AWS



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

def request_data
  @request_data
end

#request_idObject (readonly)

Request id (if exists)



18
19
20
# File 'lib/awsbase/errors.rb', line 18

def request_id
  @request_id
end

#responseObject (readonly)

Returns the value of attribute response.



26
27
28
# File 'lib/awsbase/errors.rb', line 26

def response
  @response
end

Class Method Details

.on_aws_exception(aws, options = {:raise=>true, :log=>true}) ⇒ Object

Generic handler for AwsErrors. aws is the Aws::S3, Aws::EC2, or Aws::SQS object that caused the exception (it must provide last_request and last_response). Supported boolean options are:

  • :log print a message into the log using aws.logger to access the Logger

  • :puts do a “puts” of the error

  • :raise re-raise the error after logging



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/awsbase/errors.rb', line 57

def self.on_aws_exception(aws, options={:raise=>true, :log=>true})
  # Only log & notify if not user error
  if !options[:raise] || system_error?($!)
    error_text = "#{$!.inspect}\n#{$@}.join('\n')}"
    puts error_text if options[:puts]
    # Log the error
    if options[:log]
      request   = aws.last_request ? aws.last_request.path : '-none-'
      response  = aws.last_response ? "#{aws.last_response.code} -- #{aws.last_response.message} -- #{aws.last_response.body}" : '-none-'
      @response = response
      aws.logger.error error_text
      aws.logger.error "Request was:  #{request}"
      aws.logger.error "Response was: #{response}"
    end
  end
  raise if options[:raise] # re-raise an exception
  return nil
end

.system_error?(e) ⇒ Boolean

True if e is an AWS system error, i.e. something that is for sure not the caller’s fault. Used to force logging.

Returns:

  • (Boolean)


78
79
80
# File 'lib/awsbase/errors.rb', line 78

def self.system_error?(e)
  !e.is_a?(self) || e.message =~ /InternalError|InsufficientInstanceCapacity|Unavailable/
end

Instance Method Details

#include?(pattern) ⇒ Boolean

Does any of the error messages include the regexp pattern? Used to determine whether to retry request.

Returns:

  • (Boolean)


42
43
44
45
46
47
48
49
# File 'lib/awsbase/errors.rb', line 42

def include?(pattern)
  if @errors.is_a?(Array)
    @errors.each { |code, msg| return true if code =~ pattern }
  else
    return true if @errors_str =~ pattern
  end
  false
end