Skip to content

Commit baaffb2

Browse files
author
Charlie Somerville
committed
Merge pull request BetterErrors#176 from evertrue/exception-response-codes
Exception Response Codes
2 parents f692c5e + 26d31ef commit baaffb2

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

lib/better_errors/middleware.rb

+8-3
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,10 @@ def protected_app_call(env)
8585
rescue Exception => ex
8686
@error_page = @handler.new ex, env
8787
log_exception
88-
show_error_page(env)
88+
show_error_page(env, ex)
8989
end
9090

91-
def show_error_page(env)
91+
def show_error_page(env, exception=nil)
9292
type, content = if @error_page
9393
if text?(env)
9494
[ 'plain', @error_page.render('text') ]
@@ -99,7 +99,12 @@ def show_error_page(env)
9999
[ 'html', no_errors_page ]
100100
end
101101

102-
[500, { "Content-Type" => "text/#{type}; charset=utf-8" }, [content]]
102+
status_code = 500
103+
if defined? ActionDispatch::ExceptionWrapper
104+
status_code = ActionDispatch::ExceptionWrapper.new(env, exception).status_code
105+
end
106+
107+
[status_code, { "Content-Type" => "text/#{type}; charset=utf-8" }, [content]]
103108
end
104109

105110
def text?(env)

lib/better_errors/version.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module BetterErrors
2-
VERSION = "0.9.0"
2+
VERSION = "0.10.0.pre"
33
end

spec/better_errors/middleware_spec.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module BetterErrors
44
describe Middleware do
55
let(:app) { Middleware.new(->env { ":)" }) }
6+
let(:exception) { RuntimeError.new("oh no :(") }
67

78
it "should pass non-error responses through" do
89
app.call({}).should == ":)"
@@ -54,14 +55,24 @@ module BetterErrors
5455
end
5556

5657
context "when handling an error" do
57-
let(:app) { Middleware.new(->env { raise "oh no :(" }) }
58+
let(:app) { Middleware.new(->env { raise exception }) }
5859

5960
it "should return status 500" do
6061
status, headers, body = app.call({})
6162

6263
status.should == 500
6364
end
6465

66+
it "should return ExceptionWrapper's status_code" do
67+
ad_ew = double("ActionDispatch::ExceptionWrapper")
68+
ad_ew.stub('new').with({}, exception ){ double("ExceptionWrapper", status_code: 404) }
69+
stub_const('ActionDispatch::ExceptionWrapper', ad_ew)
70+
71+
status, headers, body = app.call({})
72+
73+
status.should == 404
74+
end
75+
6576
it "should return UTF-8 error pages" do
6677
status, headers, body = app.call({})
6778

0 commit comments

Comments
 (0)