Sha256: 5bb26d63c356f8d25d439267d9ee0339e8e5dc3202cdd68433ca9c506220ac56

Contents?: true

Size: 1.44 KB

Versions: 9

Compression:

Stored size: 1.44 KB

Contents

module OpenToxError
  attr_accessor :http_code, :message, :cause
  def initialize message=nil
    message = message.to_s.gsub(/\A"|"\Z/, '') if message # remove quotes
    super message
    @http_code ||= 500
    @message = message.to_s
    @cause = cut_backtrace(caller)
    $logger.error("\n"+JSON.pretty_generate({
      :http_code => @http_code,
      :message => @message,
      :cause => @cause
    })) 
  end
  
  def cut_backtrace(trace)
    if trace.is_a?(Array)
      cut_index = trace.find_index{|line| line.match(/sinatra|minitest/)}
      cut_index ||= trace.size
      cut_index -= 1
      cut_index = trace.size-1 if cut_index < 0
      trace[0..cut_index]
    else
      trace
    end
  end

end

class RuntimeError
  include OpenToxError
end

# clutters log file with library errors 
#class NoMethodError
  #include OpenToxError
#end

module OpenTox

  class Error < RuntimeError
    include OpenToxError
    
    def initialize(code, message=nil)
      @http_code = code
      super message
    end
  end

  # OpenTox errors
  RestClientWrapper.known_errors.each do |error|
    # create error classes 
    c = Class.new Error do
      define_method :initialize do |message=nil|
        super error[:code], message
      end
    end
    OpenTox.const_set error[:class],c
    
    # define global methods for raising errors, eg. bad_request_error
    Object.send(:define_method, error[:method]) do |message|
      raise c.new(message)
    end
  end
  
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
lazar-1.1.3 lib/error.rb
lazar-1.1.0 lib/error.rb
lazar-1.0.1 lib/error.rb
lazar-1.0.0 lib/error.rb
lazar-0.9.3 lib/error.rb
lazar-0.9.2 lib/error.rb
lazar-0.9.1 lib/error.rb
lazar-0.9.0 lib/error.rb
lazar-0.0.9 lib/error.rb