@@ -212,4 +212,62 @@ def current_user_id
212212 end
213213 end
214214 end
215+
216+ describe 'handling exceptions' do
217+ let ( :app_config ) do
218+ double ( config :
219+ ActiveSupport ::OrderedOptions . new . tap do |config |
220+ config . action_dispatch = double ( rack_cache : false )
221+ config . lograge = ActiveSupport ::OrderedOptions . new
222+ end )
223+ end
224+ let ( :debug_exceptions ) do
225+ # Workaround `undefined method 'blamed_files'` bug
226+ require 'active_support/dependencies'
227+ # Workaround `undefined method 'with_indifferent_access'` bug:
228+ # https://github.com/rails/rails/issues/33634
229+ require 'active_support/core_ext/hash/indifferent_access'
230+ ActionDispatch ::DebugExceptions . new ( -> ( _ ) { raise } )
231+ end
232+ let ( :output ) { StringIO . new }
233+ let ( :logger ) { Logger . new ( output ) }
234+ let ( :env ) do
235+ Rack ::MockRequest . env_for (
236+ '' ,
237+ 'action_dispatch.show_detailed_exceptions' => true ,
238+ 'action_dispatch.logger' => logger
239+ )
240+ end
241+
242+ before do
243+ Lograge . setup ( app_config )
244+ Lograge . logger = logger
245+ end
246+
247+ it 'adds formatted exception log' do
248+ debug_exceptions . call ( env )
249+ expect ( output . string ) . to match ( /status=500 error='RuntimeError: '/ )
250+ end
251+
252+ it 'removes original exception log' do
253+ debug_exceptions . call ( env )
254+ expect ( output . string ) . not_to match ( /FATAL -- : RuntimeError/ )
255+ end
256+
257+ context 'when keep_original_rails_log is true' do
258+ before do
259+ app_config . config . lograge . keep_original_rails_log = true
260+ end
261+
262+ it 'adds formatted exception log' do
263+ debug_exceptions . call ( env )
264+ expect ( output . string ) . to match ( /status=500 error='RuntimeError: '/ )
265+ end
266+
267+ it 'keeps original exception log' do
268+ debug_exceptions . call ( env )
269+ expect ( output . string ) . to match ( /FATAL -- : RuntimeError/ )
270+ end
271+ end
272+ end
215273end
0 commit comments