@@ -10,20 +10,13 @@ module ExceptionHandler
1010 # => Attributes
1111 # => Determine schema etc
1212 ATTRS = %i( class_name status message trace target referrer params user_agent )
13-
14- # => Exceptions to be rescued by ExceptionHandler
15- EXCEPTIONS_TO_BE_RESCUED = [ ActionController ::RoutingError , AbstractController ::ActionNotFound ] . tap do |list |
16- list << ActiveRecord ::RecordNotFound if defined? ( ActiveRecord )
17- list << Mongoid ::Errors ::DocumentNotFound if defined? ( Mongoid )
18- end
1913
2014 ############################################################
2115 ############################################################
2216
2317 # => Class (inheritance dependent on whether db option is available)
24- self ::Exception = Class . new (
25- ( ExceptionHandler . config . try ( :db ) && defined? ( ActiveRecord ) ) ? ActiveRecord ::Base : Object
26- ) do
18+ self ::Exception =
19+ Class . new ( ( ExceptionHandler . config . try ( :db ) && defined? ( ActiveRecord ) ) ? ActiveRecord ::Base : Object ) do
2720
2821 # => Include individual elements
2922 # => Only required if no db present (no ActiveRecord)
@@ -97,15 +90,14 @@ def self.table_name
9790 # => Email
9891 # => after_initialize invoked after .new method called
9992 # => Should have been after_create but user may not save
100- after_initialize Proc . new { | e | ExceptionHandler ::ExceptionMailer . new_exception ( e ) . deliver } if ExceptionHandler . config . try ( :email ) . try ( :is_a? , String )
93+ after_initialize -> ( e ) { ExceptionHandler ::ExceptionMailer . new_exception ( e ) . deliver } , if : :email? # => see bottom of file
10194
10295 # => Attributes
10396 attr_accessor :request , :klass , :exception , :description
10497 attr_accessor *ATTRS unless ExceptionHandler . config . try ( :db )
10598
10699 # => Validations
107- validates :klass , exclusion : { in : EXCEPTIONS_TO_BE_RESCUED , message : "%{value}" } , if : -> { referer . blank? } # => might need full Proc syntax
108- validates :user_agent , format : { without : Regexp . new ( BOTS . join ( "|" ) , Regexp ::IGNORECASE ) }
100+ validates :user_agent , format : { without : Regexp . new ( BOTS . join ( "|" ) , Regexp ::IGNORECASE ) }
109101
110102 ##################################
111103 ##################################
@@ -114,21 +106,22 @@ def self.table_name
114106 # Virtual
115107 ####################################
116108
109+ # => Exception (virtual)
110+ # => Basis on which all the class is built
111+ def exception
112+ request . env [ 'action_dispatch.exception' ]
113+ end
114+
117115 # => Klass
118116 # => Used for validation (needs to be cleaned up in 0.7.0)
119117 def klass
120118 exception . class
121119 end
122120
123- # => Exception (virtual)
124- def exception
125- request . env [ 'action_dispatch.exception' ]
126- end
127-
128121 # => Description
129122 def description
130123 I18n . with_options scope : [ :exception_handler ] , message : message , status : status do |i18n |
131- i18n . t response , default : Rack ::Utils ::HTTP_STATUS_CODES [ status ] || status
124+ i18n . t response , default : Rack ::Utils ::HTTP_STATUS_CODES [ status ]
132125 end
133126 end
134127
@@ -143,7 +136,7 @@ def class_name
143136
144137 # => Message
145138 def message
146- exception . message
139+ exception ? exception . message : Rack :: Utils :: HTTP_STATUS_CODES [ status ]
147140 end
148141
149142 # => Trace
@@ -181,7 +174,7 @@ def user_agent
181174
182175 # => Status code (404, 500 etc)
183176 def status
184- ActionDispatch ::ExceptionWrapper . new ( request . env , exception ) . status_code
177+ exception ? ActionDispatch ::ExceptionWrapper . new ( request . env , exception ) . try ( : status_code) : request . env [ "PATH_INFO" ] [ 1 ..- 1 ] . to_i
185178 end
186179
187180 # => Server Response ("Not Found" etc)
@@ -192,6 +185,14 @@ def response
192185 ##################################
193186 ##################################
194187
188+ private
189+
190+ # => Email
191+ # => should be on the same line as after_initialize but too long
192+ def email?
193+ ExceptionHandler . config . try ( :email ) . try ( :is_a? , String ) && ExceptionHandler . config . options ( status , :notification ) != false
194+ end
195+
195196 end
196197 end
197198
0 commit comments