diff --git a/lib/io_monitor/controller.rb b/lib/io_monitor/controller.rb index 1a043c7..b9d6e1d 100644 --- a/lib/io_monitor/controller.rb +++ b/lib/io_monitor/controller.rb @@ -37,7 +37,7 @@ def append_info_to_payload(payload) data[source] = aggregator.get(source) end - data[:response] = payload[:response].body.bytesize + data[:response] = payload[:response]&.body&.bytesize || 0 end private diff --git a/lib/io_monitor/patches/net_http_adapter_patch.rb b/lib/io_monitor/patches/net_http_adapter_patch.rb index 4f3c953..a6ef49a 100644 --- a/lib/io_monitor/patches/net_http_adapter_patch.rb +++ b/lib/io_monitor/patches/net_http_adapter_patch.rb @@ -3,8 +3,8 @@ module IoMonitor module NetHttpAdapterPatch def request(*args, &block) - super do |response| - if response.body && IoMonitor.aggregator.active? + super.tap do |response| + if response&.body && IoMonitor.aggregator.active? IoMonitor.aggregator.increment(NetHttpAdapter.kind, response.body.bytesize) end end diff --git a/spec/dummy/app/controllers/fake_controller.rb b/spec/dummy/app/controllers/fake_controller.rb new file mode 100644 index 0000000..9159c53 --- /dev/null +++ b/spec/dummy/app/controllers/fake_controller.rb @@ -0,0 +1,9 @@ +# frozen_string_literal: true + +class FakeController < ApplicationController + include IoMonitor::Controller + + def fake + raise ActionController::RoutingError.new("Fake") + end +end diff --git a/spec/dummy/config/routes.rb b/spec/dummy/config/routes.rb index edf04d2..ca6b71d 100644 --- a/spec/dummy/config/routes.rb +++ b/spec/dummy/config/routes.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true Rails.application.routes.draw do + get "/fake", to: "fake#fake" end diff --git a/spec/io_monitor/controller_spec.rb b/spec/io_monitor/controller_spec.rb index 3237710..9431091 100644 --- a/spec/io_monitor/controller_spec.rb +++ b/spec/io_monitor/controller_spec.rb @@ -133,3 +133,11 @@ def show end end end + +RSpec.describe "when response is nil", type: :request do + it "does not fail" do + get "/fake" + + expect(response).to be_not_found + end +end