File tree Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Expand file tree Collapse file tree 2 files changed +32
-1
lines changed Original file line number Diff line number Diff line change 11class PingController < ApplicationController
22 def index
3- raise PG ::Error unless ActiveRecord ::Base . connection . active ?
3+ raise PG ::Error unless ActiveRecord ::Base . connectable ?
44
55 render :plain => 'pong' , :status => 200
66 end
7+
8+ private def error_handler ( e )
9+ message =
10+ case e
11+ when PG ::Error
12+ "Unable to connect to the database"
13+ else
14+ "Unknown"
15+ end
16+ message = "ERROR: #{ message } (#{ e . class . name } )"
17+ Rails . logger . error ( "#{ message } - #{ e . message } " )
18+ render :plain => message , :status => 500
19+ end
720end
Original file line number Diff line number Diff line change 77 expect ( response . status ) . to eq ( 200 )
88 expect ( response . body ) . to eq ( "pong" )
99 end
10+
11+ it 'fails gracefully with database errors' do
12+ expect ( ActiveRecord ::Base ) . to receive ( :connectable? ) . and_return ( false )
13+
14+ get :index
15+
16+ expect ( response . status ) . to eq ( 500 )
17+ expect ( response . body ) . to eq ( "ERROR: Unable to connect to the database (PG::Error)" )
18+ end
19+
20+ it 'fails gracefully with non-database errors' do
21+ expect ( ActiveRecord ::Base ) . to receive ( :connectable? ) . and_raise ( RuntimeError )
22+
23+ get :index
24+
25+ expect ( response . status ) . to eq ( 500 )
26+ expect ( response . body ) . to eq ( "ERROR: Unknown (RuntimeError)" )
27+ end
1028end
You can’t perform that action at this time.
0 commit comments