wrangler 0.1.13 → 0.1.14

Sign up to get free protection for your applications and to get access to all the features.
@@ -181,13 +181,43 @@ module Wrangler
181
181
  return nil
182
182
  end
183
183
 
184
+
185
+ # publicly available method for explicitly telling wrangler to handle
186
+ # a specific error condition without an actual exception. it's useful if you
187
+ # want to send a notification after detecting an error condition, but don't
188
+ # want to interrupt the stack by raising an exception. if you did catch an
189
+ # exception and want to do somethign similar, just call handle_exception
190
+ # diretly.
191
+ #
192
+ # the error condition will get logged and may result in notification,
193
+ # according to configuration see notify_on_exception?
194
+ #
195
+ # arguments:
196
+ # - error_messages: a message or array of messages (each gets logged on
197
+ # separate log call) capturing the error condition that
198
+ # occurred. this will get logged AND sent in any
199
+ # notifications sent
200
+ #
201
+ # options: also, any of the options accepted by handle_exception
202
+ #-----------------------------------------------------------------------------
203
+ def handle_error(error_messages, options = {})
204
+ options.merge! :error_messages => error_messages
205
+ handle_exception(nil, options)
206
+ end
207
+
208
+
184
209
  # the main exception-handling method. decides whether to notify or not,
185
210
  # whether to render an error page or not, and to make it happen.
186
211
  #
187
212
  # arguments:
188
- # - exception: the exception that was caught
213
+ # - exception: the exception that was caught. can be nil, but should
214
+ # only be nil if notifications should always be sent,
215
+ # as notification rules are bypassed this case
189
216
  #
190
217
  # options:
218
+ # :error_messages: any additional message to log and send in notification.
219
+ # can also be an array of messages (each gets logged
220
+ # separately)
191
221
  # :request: the request object (if any) that resulted in the exception
192
222
  # :render_errors: boolean indicating if an error page should be rendered
193
223
  # or not (Rails only)
@@ -199,43 +229,62 @@ module Wrangler
199
229
  request = options[:request]
200
230
  render_errors = options[:render_errors] || false
201
231
  proc_name = options[:proc_name] || config[:app_name]
232
+ error_messages = options[:error_messages]
202
233
 
203
- status_code =
204
- Wrangler::ExceptionHandler.status_code_for_exception(exception)
205
- request_data = request_data_from_request(request) unless request.nil?
206
-
207
- log_exception(exception, request_data, status_code)
208
-
209
- if exception.is_a?(Class)
210
- exception_classname = exception.name
234
+ if exception.respond_to?(:backtrace)
235
+ backtrace = exception.backtrace
211
236
  else
212
- exception_classname = exception.class.name
237
+ backtrace = caller
213
238
  end
214
239
 
215
- if exception.respond_to?(:message)
216
- exception_string = exception.message
240
+ if exception.nil?
241
+ exception_classname = nil
242
+ status_code = nil
243
+ log_error error_messages
244
+ log_error backtrace
245
+ error_string = ''
217
246
  else
218
- exception_string = exception.to_s
247
+ status_code =
248
+ Wrangler::ExceptionHandler.status_code_for_exception(exception)
249
+
250
+ request_data = request_data_from_request(request) unless request.nil?
251
+
252
+ log_exception(exception, request_data, status_code, error_messages)
253
+
254
+ if exception.is_a?(Class)
255
+ exception_classname = exception.name
256
+ else
257
+ exception_classname = exception.class.name
258
+ end
259
+
260
+ if exception.respond_to?(:message)
261
+ error_string = exception.message
262
+ else
263
+ error_string = exception.to_s
264
+ end
219
265
  end
220
266
 
221
- if notify_on_exception?(exception, status_code)
222
- if notify_with_delayed_job?
267
+ if (exception && notify_on_exception?(exception, status_code)) ||
268
+ (exception.nil? && notify_in_context?)
223
269
 
270
+ if notify_with_delayed_job?
224
271
  # don't pass in request as it contains not-easily-serializable stuff
225
272
  log_error "Wrangler sending email notification asynchronously"
226
273
  Wrangler::ExceptionNotifier.send_later(:deliver_exception_notification,
227
274
  exception_classname,
228
- exception_string,
275
+ error_string,
276
+ error_messages,
229
277
  proc_name,
230
- exception.backtrace,
278
+ backtrace,
231
279
  status_code,
232
280
  request_data)
233
281
  else
234
282
  log_error "Wrangler sending email notification synchronously"
235
283
  Wrangler::ExceptionNotifier.deliver_exception_notification(exception_classname,
236
- exception_string,
284
+ error_string,
285
+ error_messages,
237
286
  proc_name,
238
- exception.backtrace,
287
+ backtrace,
239
288
  status_code,
240
289
  request_data,
241
290
  request)
@@ -248,12 +297,12 @@ module Wrangler
248
297
  end
249
298
 
250
299
 
251
- # determine if the app is configured to notify for the given exception or
252
- # status code
300
+ # determine if the current context (local?, background) indicates that a
301
+ # notification should be sent. this applies all of the rules around
302
+ # notifications EXCEPT for what the current exception or status code is
303
+ # (see notify_on_exception? for that)
253
304
  #-----------------------------------------------------------------------------
254
- def notify_on_exception?(exception, status_code = nil)
255
- # first determine if we're configured to notify given the context of the
256
- # exception
305
+ def notify_in_context?
257
306
  if self.respond_to?(:local_request?)
258
307
  if (local_request? && config[:notify_on_local_error]) ||
259
308
  (!local_request? && config[:notify_on_public_error])
@@ -265,6 +314,18 @@ module Wrangler
265
314
  notify = config[:notify_on_background_error]
266
315
  end
267
316
 
317
+ return notify
318
+ end
319
+
320
+
321
+ # determine if the app is configured to notify for the given exception or
322
+ # status code
323
+ #-----------------------------------------------------------------------------
324
+ def notify_on_exception?(exception, status_code = nil)
325
+ # first determine if we're configured to notify given the context of the
326
+ # exception
327
+ notify = notify_in_context?
328
+
268
329
  # now if config says notify in this case, check if we're configured to
269
330
  # notify for this exception or this status code
270
331
  return notify &&
@@ -20,7 +20,7 @@ module Wrangler
20
20
  #-----------------------------------------------------------------------------
21
21
  def smtp_settings
22
22
  @@smtp_settings_overrides.reverse_merge @@smtp_settings
23
- end
23
+ end
24
24
 
25
25
  # the default configuration
26
26
  @@config ||= {
@@ -52,8 +52,12 @@ module Wrangler
52
52
  # when you call ExceptionNotifier.deliver_exception_notification())
53
53
  #
54
54
  # arguments:
55
- # - exception_classname: the class of exception that was raised
56
- # - exception_message: the error message carried by the exception
55
+ # - exception_classname: the class of exception that was raised, if any
56
+ # - error_message: the short version of the error message to display
57
+ # - additional_messages: a string or array of additional error messages
58
+ # to be logged or sent in notification...allows
59
+ # for more detail in case it won't all display
60
+ # well in email subject line, for example
57
61
  # - proc_name: the name of the process in which the exception arised
58
62
  # - backtrace: the stack trace from the exception (passing in excplicitly
59
63
  # because serializing the exception does not preserve the
@@ -70,7 +74,8 @@ module Wrangler
70
74
  # delayed_job. Optional.
71
75
  #---------------------------------------------------------------------------
72
76
  def exception_notification(exception_classname,
73
- exception_message,
77
+ error_message,
78
+ additional_messages,
74
79
  proc_name,
75
80
  backtrace,
76
81
  status_code = nil,
@@ -89,16 +94,17 @@ module Wrangler
89
94
  ensure_session_loaded(request)
90
95
 
91
96
  # NOTE: be very careful pulling data out of request in the view...it is
92
- # NOT cleaned, and may contain private data (e.g. passwords), so
97
+ # NOT cleaned, and may contain private data (e.g. passwords), so
93
98
  # scrutinize any use of @request in the views!
94
99
 
95
100
  body_hash =
96
101
  { :exception_classname => exception_classname,
97
- :exception_message => exception_message,
102
+ :error_message => error_message,
103
+ :additional_messages => additional_messages,
98
104
  :backtrace => backtrace,
99
105
  :status_code => status_code,
100
106
  :request_data => request_data,
101
- :request => request
107
+ :request => request
102
108
  }
103
109
 
104
110
  body_hash.merge! extract_data_from_request_data(request_data)
@@ -106,8 +112,8 @@ module Wrangler
106
112
  recipients config[:recipient_addresses]
107
113
  subject "[#{proc_name + (proc_name ? ' ' : '')}" +
108
114
  "#{config[:subject_prefix]}] " +
109
- "#{exception_classname}: " +
110
- "#{exception_message}"
115
+ "#{exception_classname || 'error'}: " +
116
+ "#{error_message}"
111
117
  body body_hash
112
118
  sent_on Time.now
113
119
  content_type 'text/plain'
@@ -59,7 +59,8 @@ module Wrangler
59
59
  # log the exception using logger if available. if object does not have a
60
60
  # logger, will just puts()
61
61
  #-----------------------------------------------------------------------------
62
- def log_exception(exception, request_data = nil, status_code = nil)
62
+ def log_exception(exception, request_data = nil,
63
+ status_code = nil, error_messages = nil)
63
64
  msgs = []
64
65
  msgs << "An exception was caught (#{exception.class.name}):"
65
66
 
@@ -69,6 +70,12 @@ module Wrangler
69
70
  msgs << exception.to_s
70
71
  end
71
72
 
73
+ if error_messages.is_a?(Array)
74
+ msgs.concat error_messages
75
+ elsif !error_messages.blank?
76
+ msgs << error_messages
77
+ end
78
+
72
79
  unless request_data.blank?
73
80
  msgs << "Request params were:"
74
81
  msgs << request_data.inspect
@@ -76,7 +83,7 @@ module Wrangler
76
83
  unless status_code.blank?
77
84
  msgs << "Handling with status code: #{status_code}"
78
85
  end
79
- unless exception.backtrace.blank?
86
+ if exception.respond_to?(:backtrace) && !exception.backtrace.blank?
80
87
  msgs << exception.backtrace.join("\n ")
81
88
  end
82
89
 
@@ -8,8 +8,14 @@
8
8
  <%= @protocol %><%= @host %><%= @uri %>
9
9
  <% end -%>
10
10
 
11
- A <%= @exception_classname %> occurred:
12
- * <%= @exception_message %>
11
+ A<%= @exception_classname || "n error" %> occurred:
12
+ <%= @error_message %>
13
+ <% if @additional_messages.is_a?(Array) -%>
14
+ <%= @additional_messages.join("\n ") %>
15
+ <% else -%>
16
+ <%= @additional_messages %>
17
+ <% end -%>
18
+
13
19
  <% unless @backtrace.blank? -%>
14
20
  in <%= @backtrace.first %>
15
21
  <% end -%>
data/wrangler.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{wrangler}
8
- s.version = "0.1.13"
8
+ s.version = "0.1.14"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Brian Percival"]
12
- s.date = %q{2010-02-18}
12
+ s.date = %q{2010-02-19}
13
13
  s.description = %q{A gem for handling exceptions thrown inside your Rails app. If you include the
14
14
  gem in your application controller, wrangler will render the error pages you
15
15
  configure for each exception or HTTP error code. It will also handle notifying
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wrangler
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.13
4
+ version: 0.1.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Brian Percival
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-18 00:00:00 -08:00
12
+ date: 2010-02-19 00:00:00 -08:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency