Navigation Menu

Skip to content

Commit

Permalink
Reference exception in rack.errors
Browse files Browse the repository at this point in the history
  • Loading branch information
zlw committed Jun 13, 2014
1 parent 9e64c3f commit 3c122b0
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/lotus/action/throwable.rb
Expand Up @@ -102,11 +102,19 @@ def _rescue
begin
yield
rescue => exception
_reference_in_rack_errors(exception)
_handle_exception(exception)
end
end
end

def _reference_in_rack_errors(exception)
if @_env['rack.errors']
@_env['rack.errors'].puts(exception)
@_env['rack.errors'].flush
end
end

def _handle_exception(exception)
raise unless configuration.handle_exceptions
halt configuration.exception_code(exception.class)
Expand Down
41 changes: 41 additions & 0 deletions test/integration/rack_errors_test.rb
@@ -0,0 +1,41 @@
require 'test_helper'
require 'lotus/router'

ErrorsRoutes = Lotus::Router.new do
get '/without_message', to: 'errors#without_message'
get '/with_message', to: 'errors#with_message'
end

AuthException = Class.new(StandardError)

class ErrorsController
include Lotus::Controller

action 'WithoutMessage' do
def call(params)
raise AuthException
end
end

action 'WithMessage' do
def call(params)
raise AuthException, %q{AuthException: you're not authorized to see this page!}
end
end
end

describe 'Reference exception in rack.errors' do
before do
@app = Rack::MockRequest.new(ErrorsRoutes)
end

it 'adds exception to rack.errors' do
response = @app.get('/without_message')
response.errors.must_equal "AuthException\n"
end

it 'adds exception message to rack.errors' do
response = @app.get('/with_message')
response.errors.must_equal "AuthException: you're not authorized to see this page!\n"
end
end

0 comments on commit 3c122b0

Please sign in to comment.