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
Constant Summary collapse
- FILTERED_VALUE =
'[FILTERED]'.freeze
- SENSITIVE_KEY_PATTERN =
/ authorization|cookie|password|passwd|passphrase|secret|token| api[_-]?key|credential|jwt|session|csrf|query_string|form_vars| request_uri|original_fullpath|fullpath /ix- SENSITIVE_STRING_PATTERN =
/ ( (?:authorization|cookie|password|passwd|passphrase|secret|token| api[_-]?key|credential|jwt|session|csrf) [^=:\s&;<>]{0,64} \s*(?:=|:|=>)\s*["']? ) [^&;\s<"'}]+ /ix
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
- #safe_log(context, exception) ⇒ Object
Methods inherited from Base
Constructor Details
#initialize(opts) ⇒ ExceptionMailer
Returns a new instance of ExceptionMailer.
35 36 37 38 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 35 def initialize(opts) super() @opts = opts end |
Instance Method Details
#enabled(server) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 40 def enabled(server) HaveAPI::Action.connect_hook(:exec_exception) do |ret, context, e| safe_log(context, e) ret end server.connect_hook(:description_exception) do |ret, context, e| safe_log(context, e) ret end server.connect_hook(:request_exception) do |ret, context, e| safe_log(context, e) ret end end |
#log(context, exception) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 63 def log(context, exception) request_context = context&.request req = request_context.respond_to?(:request) ? request_context.request : request_context path = request_path(context, req) frames = Array(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 frames = [Frame.new('(unknown)', 0, nil, nil)] if frames.empty? args = redact(context&.args) path_params = redact(context&.path_params) input = redact(context&.input) get = request_params(req, :GET) post = request_params(req, :POST) = (req) env = redact(request_env(request_context, req)) 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
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 107 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 |
#safe_log(context, exception) ⇒ Object
57 58 59 60 61 |
# File 'lib/haveapi/extensions/exception_mailer.rb', line 57 def safe_log(context, exception) log(context, exception) rescue StandardError => e warn "HaveAPI::Extensions::ExceptionMailer failed: #{e.class}: #{e.}" end |