Class: HaveAPI::Extensions::ExceptionMailer
- Defined in:
- lib/haveapi/extensions/exception_mailer.rb
Overview
This extension mails exceptions raised during action execution and description construction to specified e-mail address.
The template is based on Sinatra::ShowExceptions::TEMPLATE, but the JavaScript functions are removed, since e-mail doesn’t support it. HaveAPI-specific content is added. Some helper methods are taken either from Sinatra or Rack.
Defined Under Namespace
Classes: Frame
Instance Method Summary collapse
- #enabled(server) ⇒ Object
-
#initialize(opts) ⇒ ExceptionMailer
constructor
A new instance of ExceptionMailer.
- #log(context, exception) ⇒ Object
- #mail(context, exception, body) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(opts) ⇒ ExceptionMailer
Returns a new instance of ExceptionMailer.
20 21 22 23 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 20 def initialize(opts) super() @opts = opts end |
Instance Method Details
#enabled(server) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 25 def enabled(server) HaveAPI::Action.connect_hook(:exec_exception) do |ret, context, e| log(context, e) ret end server.connect_hook(:description_exception) do |ret, context, e| log(context, e) ret end end |
#log(context, exception) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 37 def log(context, exception) req = context.request.request path = (req.script_name + req.path_info).squeeze('/') frames = exception.backtrace.map do |line| frame = Frame.new next unless line =~ /(.*?):(\d+)(:in `(.*)')?/ frame.filename = ::Regexp.last_match(1) frame.lineno = ::Regexp.last_match(2).to_i frame.function = ::Regexp.last_match(4) begin lineno = frame.lineno - 1 lines = ::File.readlines(frame.filename) frame.context_line = lines[lineno].chomp rescue StandardError # ignore end frame end.compact env = context.request.env user = if context.current_user.respond_to?(:id) context.current_user.id else context.current_user end mail(context, exception, TEMPLATE.result(binding)) end |
#mail(context, exception, body) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 73 def mail(context, exception, body) mail = ::Mail.new({ from: @opts[:from], to: @opts[:to], subject: format(@opts[:subject], exception.to_s), body:, content_type: 'text/html; charset=UTF-8' }) if @opts[:smtp] mail.delivery_method(:smtp, @opts[:smtp]) else mail.delivery_method(:sendmail) end mail.deliver! mail end |