Sha256: 6b76cc81e2259a88b1d0cb3cb6fa4ff1f4a35de3195b739d357d206fec6a2a57

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

require File.dirname(__FILE__) + '/../spec/aquarium/spec_helper'
require 'aquarium'

# Example demonstrating "wrapping" an exception; rescuing an exception and 
# throwing a different one. A common use for this is to map exceptions across
# "domain" boundaries, e.g., persistence and application logic domains. 
# Note that you must use :around advice, since :after_raising cannot change
# the control flow.
# (However, see feature request #19119)

module Aquarium
  class Exception1 < Exception; end
  class Exception2 < Exception; end
  class NewException < Exception; end

  class Raiser
    def raise_exception1
      raise Exception1.new("one")
    end
    def raise_exception2
      raise Exception2.new("two")
    end
  end
end

describe "Rescuing one exception type and raising a second type" do
  before :all do
    @aspect = Aquarium::Aspects::Aspect.new :around, 
      :calls_to => /^raise_exception/, :in_type => Aquarium::Raiser do |jp, obj, *args|
      begin
        jp.proceed
      rescue Aquarium::Exception1 => e
        raise Aquarium::NewException.new("New Exception: old exception message was: #{e.message}")
      end
    end
    @raiser = Aquarium::Raiser.new
  end
  
  after :all do
    @aspect.unadvise
  end
  
  it "should intercept the specified type of exception" do
    lambda { @raiser.raise_exception1 }.should raise_error(Aquarium::NewException)
  end
  it "should not intercept other types of exceptions" do
    lambda { @raiser.raise_exception2 }.should raise_error(Aquarium::Exception2)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
aquarium-0.5.1 examples/exception_wrapping_example_spec.rb
aquarium-0.4.4 examples/exception_wrapping_example_spec.rb
aquarium-0.4.1 examples/exception_wrapping_example_spec.rb
aquarium-0.4.2 examples/exception_wrapping_example_spec.rb
aquarium-0.4.3 examples/exception_wrapping_example_spec.rb