Exception: Rightscale::GogridError

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

Overview

Exception class to signal any GoGrid errors. All errors occuring during calls to GoGrid’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) ⇒ GogridError

Returns a new instance of GogridError.



321
322
323
324
325
# File 'lib/gogrid_base.rb', line 321

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 GogridError.new('err_text')



316
317
318
# File 'lib/gogrid_base.rb', line 316

def errors
  @errors
end

#http_codeObject (readonly)

Response HTTP error code



319
320
321
# File 'lib/gogrid_base.rb', line 319

def http_code
  @http_code
end

Class Method Details

.on_gogrid_exception(gogrid, options = {:raise=>true, :log=>true}) ⇒ Object

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

  • :puts do a “puts” of the error

  • :raise re-raise the error after logging



344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# File 'lib/gogrid_base.rb', line 344

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

Returns:

  • (Boolean)


365
366
367
# File 'lib/gogrid_base.rb', line 365

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)


329
330
331
332
333
334
335
336
# File 'lib/gogrid_base.rb', line 329

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