From 29f8a3bc6a9859aad3708618d3de2b6a0b97482c Mon Sep 17 00:00:00 2001 From: Johan Kok Date: Tue, 2 Jul 2019 10:50:17 +0200 Subject: [PATCH] Added error handler for Rollbar --- Gemfile | 1 + lib/hutch/error_handlers.rb | 1 + lib/hutch/error_handlers/rollbar.rb | 28 ++++++++++++++ spec/hutch/error_handlers/rollbar_spec.rb | 45 +++++++++++++++++++++++ 4 files changed, 75 insertions(+) create mode 100644 lib/hutch/error_handlers/rollbar.rb create mode 100644 spec/hutch/error_handlers/rollbar_spec.rb diff --git a/Gemfile b/Gemfile index c860ba66..3d0cdf73 100644 --- a/Gemfile +++ b/Gemfile @@ -24,6 +24,7 @@ group :development, :test do gem "coveralls", "~> 0.8.15", require: false gem "newrelic_rpm" gem "airbrake", "~> 9.0" + gem "rollbar" end group :development, :darwin do diff --git a/lib/hutch/error_handlers.rb b/lib/hutch/error_handlers.rb index deaff3b7..36a238f9 100644 --- a/lib/hutch/error_handlers.rb +++ b/lib/hutch/error_handlers.rb @@ -4,5 +4,6 @@ module ErrorHandlers autoload :Sentry, 'hutch/error_handlers/sentry' autoload :Honeybadger, 'hutch/error_handlers/honeybadger' autoload :Airbrake, 'hutch/error_handlers/airbrake' + autoload :Rollbar, 'hutch/error_handlers/rollbar' end end diff --git a/lib/hutch/error_handlers/rollbar.rb b/lib/hutch/error_handlers/rollbar.rb new file mode 100644 index 00000000..501bce0a --- /dev/null +++ b/lib/hutch/error_handlers/rollbar.rb @@ -0,0 +1,28 @@ +require 'hutch/logging' +require 'rollbar' +require 'hutch/error_handlers/base' + +module Hutch + module ErrorHandlers + class Rollbar < Base + def handle(properties, payload, consumer, ex) + message_id = properties.message_id + prefix = "message(#{message_id || '-'}):" + logger.error "#{prefix} Logging event to Rollbar" + logger.error "#{prefix} #{ex.class} - #{ex.message}" + + ::Rollbar.error(ex, + payload: payload, + consumer: consumer + ) + end + + def handle_setup_exception(ex) + logger.error "Logging setup exception to Rollbar" + logger.error "#{ex.class} - #{ex.message}" + + ::Rollbar.error(ex) + end + end + end +end diff --git a/spec/hutch/error_handlers/rollbar_spec.rb b/spec/hutch/error_handlers/rollbar_spec.rb new file mode 100644 index 00000000..be527b90 --- /dev/null +++ b/spec/hutch/error_handlers/rollbar_spec.rb @@ -0,0 +1,45 @@ +require 'spec_helper' + +describe Hutch::ErrorHandlers::Rollbar do + let(:error_handler) { Hutch::ErrorHandlers::Rollbar.new } + + describe '#handle' do + let(:error) do + begin + raise "Stuff went wrong" + rescue RuntimeError => err + err + end + end + + it "logs the error to Rollbar" do + message_id = "1" + properties = OpenStruct.new(message_id: message_id) + payload = "{}" + consumer = double + ex = error + message = { + payload: payload, + consumer: consumer + } + expect(::Rollbar).to receive(:error).with(ex, message) + error_handler.handle(properties, payload, consumer, ex) + end + end + + describe '#handle_setup_exception' do + let(:error) do + begin + raise "Stuff went wrong" + rescue RuntimeError => err + err + end + end + + it "logs the error to Rollbar" do + ex = error + expect(::Rollbar).to receive(:error).with(ex) + error_handler.handle_setup_exception(ex) + end + end +end