Sha256: 516ee7f5187b60926af1ac38e83994f3408036a775c4f6495b3092c2e3510ac0
Contents?: true
Size: 1.25 KB
Versions: 1
Compression:
Stored size: 1.25 KB
Contents
# frozen_string_literal: true # rescue from errors # # in some class # Lux::RescueFrom.define(self) -> defines rescue_from method in class # # to add # rescue_from(UnauthorizedError) { ... } # # and to resolve in the end # @@rescue_from_ivar.call { main } class Lux::RescueFrom class << self def define(klass) ClassAttributes.define klass, :rescue_from_hash klass.class_eval %[ @@rescue_from_ivar = Lux::RescueFrom.new def self.rescue_from(name, &block) @@rescue_from_ivar.add(name, &block) end ] end end ### def initialize @rescues = {} end def add(name, &block) die ":#{name} is not allowed rescue name, only allowed are :default and :allways" if name.is_symbol? && ![:default, :allways].index(name) @rescues[name.to_s] = block end def call begin yield rescue Exception => e raise e unless Lux.page return if Lux.page.body && Lux.page.status if proc = @rescues[e.class.to_s] proc.call(e.message) elsif @rescues['default'] # catch them all @rescues['default'].call(e.message) else raise e end @rescues['allways'].call(e.message) if @rescues['allways'] Lux.page.status e.class end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
lux-fw-0.1.17 | ./lib/lux/rescue_from/rescue_from.rb |