Sha256: 39305463ea96a8a7302b645314e0f788bdde63f5bfd99d6a57a3473a4c65c307

Contents?: true

Size: 935 Bytes

Versions: 7

Compression:

Stored size: 935 Bytes

Contents

require 'unstrict_proc'

class JSONAPIonify::CustomRescue
  using UnstrictProc

  def self.perform(**opts, &block)
    new(**opts, &block).perform
  end

  def initialize(remove: [], source: nil, formatter:, &block)
    @block = block
    @formatter = formatter
    @source = source || block
    @locs = Array.wrap remove
    f, l = self.class.method(:perform).source_location
    @locs << [f, l+1].join(':')
  end

  def source_location
    source.source_location
  end

  def perform
    @block.call
  rescue => e
    loc = [__FILE__, __LINE__-2].join(':')
    formatted_value = @formatter.unstrict.call(Error.new(e, @source))
    index = e.backtrace.index { |l| l.include? loc }
    e.backtrace[index] = formatted_value if index && formatted_value
    e.backtrace.delete_if { |l| @locs.any? { |rl| l.include? rl } }
    raise e
  end

  class Error < Struct.new :error, :source
    delegate :source_location, to: :source
  end

end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
jsonapionify-0.12.10 lib/jsonapionify/custom_rescue.rb
jsonapionify-0.12.9 lib/jsonapionify/custom_rescue.rb
jsonapionify-0.12.8 lib/jsonapionify/custom_rescue.rb
jsonapionify-0.12.7 lib/jsonapionify/custom_rescue.rb
jsonapionify-0.12.5 lib/jsonapionify/custom_rescue.rb
jsonapionify-0.12.4 lib/jsonapionify/custom_rescue.rb
jsonapionify-0.12.3 lib/jsonapionify/custom_rescue.rb