Skip to content

Commit

Permalink
Allow exception handling to be disabled
Browse files Browse the repository at this point in the history
The default exception handling is very inconvenient in development mode
as it may hide difficult to spot errors behind a very generic 500 error.
  • Loading branch information
sidonath committed Mar 17, 2014
1 parent 9a3758a commit 6da21f9
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions lib/lotus/action/throwable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,19 @@ def self.extended(base)
# @see Lotus::Action::Throwable.handle_exception
class_attribute :handled_exceptions
self.handled_exceptions = Controller.handled_exceptions.dup

# Action-level setting for handling exceptions.
#
# When an exception is raised during a #call execution it will be
# translated into an associated HTTP status by default. You may
# want to disable this behavior for your testes.
#
# @api private
# @since 0.2.0
#
# @see Lotus::Controller.handle_exceptions
class_attribute :handle_exceptions
self.handle_exceptions = Controller.handle_exceptions
end
end

Expand Down Expand Up @@ -123,6 +136,7 @@ def _rescue
end

def _handle_exception(exception)
raise unless self.class.handle_exceptions
throw self.class.handled_exceptions.fetch(exception.class, 500)
end
end
Expand Down
31 changes: 31 additions & 0 deletions lib/lotus/controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@ module Controller
class_attribute :handled_exceptions
self.handled_exceptions = {}

# Global setting for handling exceptions.
#
# When an exception is raised during a #call execution it will be
# translated into an associated HTTP status by default. You may want to
# disable this behavior for your testes.
#
# **Important:** Be sure to set this configuration, **before** the actions
# and controllers of your application are loaded.
#
# @since 0.2.0
#
# @see Lotus::Action::Throwable
#
# @example
# require 'lotus/controller'
#
# Lotus::Controller.handle_exceptions = false
#
# class Show
# include Lotus::Action
#
# def call(params)
# # ...
# raise RecordNotFound.new
# end
# end
#
# Show.new.call({id: 1}) # => ERROR RecordNotFound: RecordNotFound
class_attribute :handle_exceptions
self.handle_exceptions = true

def self.included(base)
base.class_eval do
include Dsl
Expand Down
16 changes: 16 additions & 0 deletions test/action_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@
response[0].must_equal 500
response[2].must_equal ['Internal Server Error']
end

describe 'when exception handling code is disabled' do
before do
ErrorCallAction.handle_exceptions = false
end

after do
ErrorCallAction.handle_exceptions = true
end

it 'should raise an actual exception' do
proc {
ErrorCallAction.new.call({})
}.must_raise RuntimeError
end
end
end

describe '#expose' do
Expand Down

0 comments on commit 6da21f9

Please sign in to comment.