Sha256: 33ee18ccb4c05e027a3e5bff470292160e91fb2a82e47ca028660c7150255be9

Contents?: true

Size: 1.78 KB

Versions: 5

Compression:

Stored size: 1.78 KB

Contents

require 'rubygems'
require 'require_relative' if RUBY_VERSION < '1.9'
require_relative './common'
require_relative '../../../lib/deltacloud/drivers/exceptions'

class TestException < StandardError; end

class ExceptionTestClass
  include Deltacloud::Exceptions

  def raise_exception(id)
    case id
      when 1 then safely { raise 'test1 exception' }
      when 2 then safely { raise TestException }
      when 3 then safely { raise 'not captured' }
    end
  end

  exceptions do
    on /test1/ do
      status 500
      message 'Test1ErrorMessage'
    end
    on TestException do
      status 400
      message 'StandardErrorTest'
    end
  end
end

def raise_error(id); ExceptionTestClass.new.raise_exception(id); end

describe Deltacloud::Exceptions do

  it 'should capture exception when match the exception message' do
    lambda { raise_error 1 }.must_raise Deltacloud::Exceptions::BackendError

    begin raise_error(1); rescue Deltacloud::Exceptions::BackendError => e
      e.code.must_equal 500
      e.message.must_equal 'Test1ErrorMessage'
      e.backtrace.wont_be_empty
    end

  end

  it 'should capture exception when match the exception class' do
    lambda { raise_error 2 }.must_raise Deltacloud::Exceptions::ValidationFailure
    begin raise_error(2); rescue Deltacloud::Exceptions::ValidationFailure => e
      e.code.must_equal 400
      e.message.must_equal 'StandardErrorTest'
      e.backtrace.wont_be_empty
    end
  end

  it 'should capture exception when no match found' do
    lambda { raise_error 3 }.must_raise Deltacloud::Exceptions::BackendError
    begin raise_error(3); rescue Deltacloud::Exceptions::BackendError => e
      e.code.must_equal 500
      e.message.must_equal 'Unhandled exception or status code (not captured)'
      e.backtrace.wont_be_empty
    end
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
deltacloud-core-1.1.3 tests/drivers/base/exceptions_test.rb
deltacloud-core-1.1.2 tests/drivers/base/exceptions_test.rb
deltacloud-core-1.1.1 tests/drivers/base/exceptions_test.rb
deltacloud-core-1.1.0 tests/drivers/base/exceptions_test.rb
deltacloud-core-1.0.5 tests/drivers/base/exceptions_test.rb