Exception: Rightscale::SlicehostError

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

Overview

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

message    - the text of the error

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(errors = nil, http_code = nil) ⇒ SlicehostError

Returns a new instance of SlicehostError.



273
274
275
276
277
# File 'lib/slicehost_base.rb', line 273

def initialize(errors=nil, http_code=nil)
  @errors      = errors
  @http_code   = http_code
  super(@errors.is_a?(Array) ? @errors.map{|code, msg| "#{code}: #{msg}"}.join("; ") : @errors.to_s)
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 SlicehostError.new('err_text')



268
269
270
# File 'lib/slicehost_base.rb', line 268

def errors
  @errors
end

#http_codeObject (readonly)

Response HTTP error code



271
272
273
# File 'lib/slicehost_base.rb', line 271

def http_code
  @http_code
end

Class Method Details

.on_slicehost_exception(slicehost, options = {:raise=>true, :log=>true}) ⇒ Object

Generic handler for SlicehostErrors. 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 slicehost.logger to access the Logger

  • :puts do a “puts” of the error

  • :raise re-raise the error after logging



296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
# File 'lib/slicehost_base.rb', line 296

def self.on_slicehost_exception(slicehost, 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  = slicehost.last_request  ? slicehost.last_request.path :  '-none-'
      response = slicehost.last_response ? "#{slicehost.last_response.code} -- #{slicehost.last_response.message} -- #{slicehost.last_response.body}" : '-none-'
      slicehost.logger.error error_text
      slicehost.logger.error "Request was:  #{request}"
      slicehost.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 Slicehost system error, i.e. something that is for sure not the caller’s fault. Used to force logging. TODO: Place Slicehost Errors here - these are AWS errs

Returns:

  • (Boolean)


317
318
319
# File 'lib/slicehost_base.rb', line 317

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)


281
282
283
284
285
286
287
288
# File 'lib/slicehost_base.rb', line 281

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