Sha256: ab4c016d20ccd466e7590f4819512de0713e6ee85aa26b26ab35f99d3a2b31ae

Contents?: true

Size: 1.87 KB

Versions: 8

Compression:

Stored size: 1.87 KB

Contents

class Wolverine
  # Reformats errors raised by redis representing failures while executing
  # a lua script. The default errors have confusing messages and backtraces,
  # and a type of +RuntimeError+. This class improves the message and
  # modifies the backtrace to include the lua script itself in a reasonable
  # way.
  class LuaError < StandardError
    PATTERN = /ERR Error (compiling|running) script \(.*?\): .*?:(\d+): (.*)/
    WOLVERINE_LIB_PATH = File.expand_path('../../', __FILE__)

    # Is this error one that should be reformatted?
    #
    # @param error [StandardError] the original error raised by redis
    # @return [Boolean] is this an error that should be reformatted?
    def self.intercepts? error
      error.message =~ PATTERN
    end

    # Initialize a new {LuaError} from an existing redis error, adjusting
    # the message and backtrace in the process.
    #
    # @param error [StandardError] the original error raised by redis
    # @param file [Pathname] full path to the lua file the error ocurred in
    def initialize error, file
      @error = error
      @file = file

      @error.message =~ PATTERN
      stage, line_number, message = $1, $2, $3

      super message
      set_backtrace generate_backtrace file, line_number
    end

    private

    def generate_backtrace(file, line_number)
      pre_wolverine = backtrace_before_entering_wolverine(@error.backtrace)
      index_of_first_wolverine_line = (@error.backtrace.size - pre_wolverine.size - 1)
      pre_wolverine.unshift(@error.backtrace[index_of_first_wolverine_line])
      pre_wolverine.unshift("#{file}:#{line_number}")
      pre_wolverine
    end

    def backtrace_before_entering_wolverine(backtrace)
      backtrace.reverse.take_while { |line| ! line_from_wolverine(line) }.reverse
    end

    def line_from_wolverine(line)
      line.split(':').first.include?(WOLVERINE_LIB_PATH)
    end

  end

end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
wolverine-0.3.4 lib/wolverine/lua_error.rb
wolverine-0.3.3 lib/wolverine/lua_error.rb
wolverine-0.3.2 lib/wolverine/lua_error.rb
wolverine-0.3.1 lib/wolverine/lua_error.rb
wolverine-0.2.7 lib/wolverine/lua_error.rb
wolverine-0.2.6 lib/wolverine/lua_error.rb
wolverine-0.3.0 lib/wolverine/lua_error.rb
wolverine-0.2.5 lib/wolverine/lua_error.rb