Module: ForestErrorsHelper

Defined in:
app/helpers/forest_errors_helper.rb

Instance Method Summary collapse

Instance Method Details

#forest_admin_error(error, options = {}) ⇒ Object

Display errors that are only visible to admin users



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/helpers/forest_errors_helper.rb', line 3

def forest_admin_error(error, options = {})
  if Rails.env.production?
    if current_user&.admin?
      logger.error("[Forest] forest_admin_error")
      logger.error(error.message)
      logger.error(error.backtrace.join("\n"))

      error_messages = []
      error_messages << "Error on #{Rails.application.class.parent.to_s.titleize}\n"
      error_messages << "Path: #{request.fullpath.force_encoding('utf-8')}\n"
      error_messages << "#{error.message}\n"
      error_messages << error.backtrace.first(20).join("\n")

      visible_errors_for_admin = []
      visible_errors_for_admin << options.fetch(:message, "Warning: this code block has errors and is not visible to the public.\nYou are seeing this message because you are an admin.\n")
      visible_errors_for_admin << mail_to('mailto:[email protected]',
                                      "Click here to email this error to the developers.\n",
                                      subject: "Error on #{Rails.application.class.parent.to_s.titleize} - #{request.path.force_encoding('utf-8')}",
                                      body: error_messages.join("\n").html_safe,
                                      target: '_blank')
      visible_errors_for_admin << "#{error.message}\n"
      visible_errors_for_admin << error.backtrace.join("\n")

       :pre, class: 'admin-error-message' do
        visible_errors_for_admin.join("\n").html_safe
      end
    end
  else
    raise error
  end
end