@@ -10,20 +10,13 @@ module ExceptionHandler
10
10
# => Attributes
11
11
# => Determine schema etc
12
12
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
19
13
20
14
############################################################
21
15
############################################################
22
16
23
17
# => 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
27
20
28
21
# => Include individual elements
29
22
# => Only required if no db present (no ActiveRecord)
@@ -97,15 +90,14 @@ def self.table_name
97
90
# => Email
98
91
# => after_initialize invoked after .new method called
99
92
# => 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
101
94
102
95
# => Attributes
103
96
attr_accessor :request , :klass , :exception , :description
104
97
attr_accessor *ATTRS unless ExceptionHandler . config . try ( :db )
105
98
106
99
# => 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 ) }
109
101
110
102
##################################
111
103
##################################
@@ -114,21 +106,22 @@ def self.table_name
114
106
# Virtual
115
107
####################################
116
108
109
+ # => Exception (virtual)
110
+ # => Basis on which all the class is built
111
+ def exception
112
+ request . env [ 'action_dispatch.exception' ]
113
+ end
114
+
117
115
# => Klass
118
116
# => Used for validation (needs to be cleaned up in 0.7.0)
119
117
def klass
120
118
exception . class
121
119
end
122
120
123
- # => Exception (virtual)
124
- def exception
125
- request . env [ 'action_dispatch.exception' ]
126
- end
127
-
128
121
# => Description
129
122
def description
130
123
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 ]
132
125
end
133
126
end
134
127
@@ -143,7 +136,7 @@ def class_name
143
136
144
137
# => Message
145
138
def message
146
- exception . message
139
+ exception ? exception . message : Rack :: Utils :: HTTP_STATUS_CODES [ status ]
147
140
end
148
141
149
142
# => Trace
@@ -181,7 +174,7 @@ def user_agent
181
174
182
175
# => Status code (404, 500 etc)
183
176
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
185
178
end
186
179
187
180
# => Server Response ("Not Found" etc)
@@ -192,6 +185,14 @@ def response
192
185
##################################
193
186
##################################
194
187
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
+
195
196
end
196
197
end
197
198
0 commit comments