Skip to content

Commit b8570b0

Browse files
authored
Merge pull request #9621 from Fryguy/fix_ping_controller
Better PingController errors
2 parents 948020b + 8b67d07 commit b8570b0

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

app/controllers/ping_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
class PingController < ApplicationController
22
def index
3-
raise PG::Error unless ActiveRecord::Base.connectable?
3+
ActiveRecord::Base.connectable!
44

55
render :plain => 'pong', :status => 200
66
end
77

88
private def error_handler(e)
99
message =
1010
case e
11-
when PG::Error
11+
when *ActiveRecord::Base::CONNECTIVITY_ERRORS
1212
"Unable to connect to the database"
1313
else
1414
"Unknown"

spec/controllers/ping_controller_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@
99
end
1010

1111
it 'fails gracefully with database errors' do
12-
expect(ActiveRecord::Base).to receive(:connectable?).and_return(false)
12+
expect(ActiveRecord::Base).to receive(:connectable!).and_raise(PG::ConnectionBad)
1313

1414
get :index
1515

1616
expect(response.status).to eq(500)
17-
expect(response.body).to eq("ERROR: Unable to connect to the database (PG::Error)")
17+
expect(response.body).to eq("ERROR: Unable to connect to the database (PG::ConnectionBad)")
1818
end
1919

2020
it 'fails gracefully with non-database errors' do
21-
expect(ActiveRecord::Base).to receive(:connectable?).and_raise(RuntimeError)
21+
expect(ActiveRecord::Base).to receive(:connectable!).and_raise(RuntimeError)
2222

2323
get :index
2424

0 commit comments

Comments
 (0)