Skip to content

Commit

Permalink
Change exception handler to be pluggable
Browse files Browse the repository at this point in the history
  • Loading branch information
Takashi Kokubun committed Sep 30, 2016
1 parent c04b386 commit bd828aa
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 17 deletions.
2 changes: 1 addition & 1 deletion lib/barbeque/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
module Barbeque
module Configuration
DEFAULT_CONFIG = {
'exception_handler' => 'rails_logger',
'exception_handler' => 'RailsLogger',
'runner' => 'Docker',
}

Expand Down
27 changes: 12 additions & 15 deletions lib/barbeque/exception_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,21 @@

module Barbeque
module ExceptionHandler
class << self
def handle_exception(e)
case Barbeque.config.exception_handler
when 'rails_logger'
rails_logger(e)
when 'raven'
Raven.capture_exception(e)
else
Rails.logger.fatal("Unexpected exception handler: #{Barbeque.config.exception_handler}")
rails_logger(e)
end
end

private
def self.handle_exception(e)
handler = const_get(Barbeque.config.exception_handler, false)
handler.handle_exception(e)
end

def rails_logger(e)
module RailsLogger
def self.handle_exception(e)
Rails.logger.error("#{e.inspect}\n#{e.backtrace.join("\n")}")
end
end

module Raven
def self.handle_exception(e)
::Raven.capture_exception(e)
end
end
end
end
22 changes: 22 additions & 0 deletions spec/barbeque/exception_handler_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
require 'rails_helper'
require 'barbeque/exception_handler'

describe Barbeque::ExceptionHandler do
describe '.handle_exception' do
let(:exception) do
StandardError.new('something went wrong').tap do |e|
e.set_backtrace(['barbeque.rb:1'])
end
end
let(:handler) { 'RailsLogger' }

before do
allow(Barbeque).to receive_message_chain(:config, :exception_handler).and_return(handler)
end

it 'handles exception with configured handler' do
expect(Barbeque::ExceptionHandler::RailsLogger).to receive(:handle_exception).with(exception)
Barbeque::ExceptionHandler.handle_exception(exception)
end
end
end
2 changes: 1 addition & 1 deletion spec/dummy/config/barbeque.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
default: &default
exception_handler: rails_logger
exception_handler: RailsLogger
runner: Docker

development:
Expand Down

0 comments on commit bd828aa

Please sign in to comment.