|
| 1 | +require 'pact_broker/api/resources/error_handler' |
| 2 | + |
| 3 | +module PactBroker |
| 4 | + module Api |
| 5 | + module Resources |
| 6 | + describe ErrorHandler do |
| 7 | + describe "call" do |
| 8 | + let(:error) { PactBroker::Error.new('test error') } |
| 9 | + let(:thing) { double('thing', call: nil, another_call: nil) } |
| 10 | + let(:options) { { env: env } } |
| 11 | + let(:request) { double('request' ) } |
| 12 | + let(:response) { double('response', :body= => nil) } |
| 13 | + let(:env) { double('env') } |
| 14 | + |
| 15 | + subject { ErrorHandler.call(error, request, response) } |
| 16 | + |
| 17 | + before do |
| 18 | + allow(Webmachine::ConvertRequestToRackEnv).to receive(:call).and_return(env) |
| 19 | + PactBroker.configuration.add_api_error_reporter do | error, options | |
| 20 | + thing.call(error, options) |
| 21 | + end |
| 22 | + |
| 23 | + PactBroker.configuration.add_api_error_reporter do | error, options | |
| 24 | + thing.another_call(error, options) |
| 25 | + end |
| 26 | + end |
| 27 | + |
| 28 | + it "invokes the api error reporters" do |
| 29 | + expect(thing).to receive(:call).with(error, options) |
| 30 | + expect(thing).to receive(:another_call).with(error, options) |
| 31 | + subject |
| 32 | + end |
| 33 | + |
| 34 | + context "when the error reporter raises an error itself" do |
| 35 | + class TestError < StandardError; end |
| 36 | + |
| 37 | + before do |
| 38 | + expect(thing).to receive(:call).and_raise(TestError.new) |
| 39 | + end |
| 40 | + |
| 41 | + it "logs the error" do |
| 42 | + expect(PactBroker.logger).to receive(:error).at_least(1).times |
| 43 | + subject |
| 44 | + end |
| 45 | + |
| 46 | + it "does not propagate the error" do |
| 47 | + expect(thing).to receive(:another_call) |
| 48 | + subject |
| 49 | + end |
| 50 | + end |
| 51 | + end |
| 52 | + end |
| 53 | + end |
| 54 | + end |
| 55 | +end |
0 commit comments