Class: AsanaExceptionNotifier::ErrorPage

Inherits:
Object
  • Object
show all
Includes:
ApplicationHelper
Defined in:
lib/asana_exception_notifier/classes/error_page.rb

Overview

class used for rendering the template for exception

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ApplicationHelper

add_files_to_zip, compress_files, create_archive, create_upload_file_part, default_template_path, ensure_eventmachine_running, escape, execute_with_rescue, extract_body, file_upload_request_options, force_utf8_encoding, get_extension_and_name_from_file, get_hash_rows, get_multi_request_values, get_response_from_request, hash_to_html_attributes, link_helper, log_bactrace, log_exception, logger, max_length, mount_table, mount_table_for_hash, multi_request_manager, multipart_file_upload_details, parse_fieldset_value, permitted_options, register_em_error_handler, rescue_interrupt, root, run_em_reactor, set_fieldset_key, setup_em_options, show_hash_content, split_archive, tempfile_details, template_dir, template_path_exist

Constructor Details

#initialize(template_path, exception, options) ⇒ ErrorPage

Returns a new instance of ErrorPage.



9
10
11
12
13
14
15
16
17
18
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 9

def initialize(template_path, exception, options)
  @template_path = template_path
  @exception = exception
  @options = options.symbolize_keys
  @template_details = setup_template_details
  @env = (@options[:env] || ENV.to_h).stringify_keys
  @request = action_dispatch? ? ActionDispatch::Request.new(@env) : Rack::Request.new(@env)
  @timestamp = Time.now
  parse_exception_options
end

Instance Attribute Details

#boundaryObject (readonly)

Returns the value of attribute boundary.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def boundary
  @boundary
end

#contentObject (readonly)

Returns the value of attribute content.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def content
  @content
end

#envObject (readonly)

Returns the value of attribute env.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def env
  @env
end

#exceptionObject (readonly)

Returns the value of attribute exception.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def exception
  @exception
end

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def options
  @options
end

#requestObject (readonly)

Returns the value of attribute request.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def request
  @request
end

#tempfileObject (readonly)

Returns the value of attribute tempfile.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def tempfile
  @tempfile
end

#template_detailsObject (readonly)

Returns the value of attribute template_details.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def template_details
  @template_details
end

#template_paramsObject (readonly)

Returns the value of attribute template_params.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def template_params
  @template_params
end

#template_pathObject (readonly)

Returns the value of attribute template_path.



7
8
9
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 7

def template_path
  @template_path
end

Instance Method Details

#action_dispatch?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 20

def action_dispatch?
  defined?(ActionDispatch::Request)
end


92
93
94
95
96
97
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 92

def add_to_links(links, prefix, options = {})
  expected_value = parse_fieldset_value(options)
  return unless expected_value.present?
  prefix_name = set_fieldset_key(links, prefix || 'basic_info')
  links[prefix_name][options[:key]] = expected_value
end

#create_tempfile(output = render_template) ⇒ Object



110
111
112
113
114
115
116
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 110

def create_tempfile(output = render_template)
  tempfile = Tempfile.new([SecureRandom.uuid, ".#{@template_details[:template_extension]}"], encoding: 'utf-8')
  tempfile.write(output)
  tempfile.close
  details = tempfile_details(tempfile)
  [details[:filename], details[:path]]
end

#exception_dataObject



48
49
50
51
52
53
54
55
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 48

def exception_data
  {
    error_class: @exception.class.to_s,
    message:  @exception.respond_to?(:message) ? @exception.message : exception.inspect,
    backtrace: @exception.respond_to?(:backtrace) ? @exception.backtrace : '',
    cause: @exception.respond_to?(:cause) ? @exception.cause : ''
  }
end

#fetch_archives(output = render_template) ⇒ Object



118
119
120
121
122
123
124
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 118

def fetch_archives(output = render_template)
  return [] if output.blank?
  filename, path = create_tempfile(output)
  archive = compress_files(File.dirname(path), filename, [path])
  remove_tempfile(path)
  split_archive(archive, "part_#{filename}", 1024 * 1024 * 100)
end

#fetch_fieldsets(hash, links = {}, prefix = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 80

def fetch_fieldsets(hash, links = {}, prefix = nil)
  return unless hash.is_a?(Hash)
  hash.each do |key, value|
    if value.is_a?(Hash)
      fetch_fieldsets(value, links, key)
    else
      add_to_links(links, prefix, key: key, value: value)
    end
  end
  links
end

#fieldsetsObject



99
100
101
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 99

def fieldsets
  @fieldsets ||= fetch_fieldsets(parse_exception_options).except(:env)
end


76
77
78
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 76

def fieldsets_links
  fieldsets.map { |key, _value| link_helper(key.to_s) }.join(' | ')
end

#parse_exception_optionsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 31

def parse_exception_options
  @template_params ||= {
    server:  Socket.gethostname,
    exception: @exception,
    request: @request,
    environment: action_dispatch? ? @request.filtered_env : @env,
    rails_root: defined?(Rails) ? Rails.root : nil,
    process: $PROCESS_ID,
    data: (@env.blank? ? {} : @env.fetch(:'exception_notifier.exception_data', {})).merge(@options[:data] || {}),
    exception_data: exception_data,
    request_data: setup_env_params,
    uname: Sys::Uname.uname,
    timestamp: @timestamp,
    pwd:  File.expand_path($PROGRAM_NAME)
  }.merge(@options).reject { |_key, value| value.blank? }
end

#remove_tempfile(path) ⇒ Object



126
127
128
129
130
131
132
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 126

def remove_tempfile(path)
  if ENV['DEBUG_ASANA_TEMPLATE']
    logger.debug(path)
  else
    FileUtils.rm_rf([path])
  end
end

#render_template(template = nil) ⇒ Object



103
104
105
106
107
108
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 103

def render_template(template = nil)
  execute_with_rescue do
    current_template = template.present? ? template : @template_path
    Tilt.new(current_template).render(self, @template_params.stringify_keys)
  end
end

#request_paramsObject



70
71
72
73
74
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 70

def request_params
  @request.params
rescue
  {}
end

#setup_env_paramsObject



57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 57

def setup_env_params
  {
    url: action_dispatch? ? @request.original_url : @request.path_info,
    referrer: @request.referer,
    http_method: action_dispatch? ? @request.method : @request.request_method,
    ip_address:  action_dispatch? ? @request.remote_ip : @request.ip,
    parameters: action_dispatch? ? @request.filtered_parameters : request_params,
    session: @request.session,
    cookies: @request.cookies,
    user_agent: @request.user_agent
  }
end

#setup_template_detailsObject



24
25
26
27
28
29
# File 'lib/asana_exception_notifier/classes/error_page.rb', line 24

def setup_template_details
  template_extension = @template_path.scan(/\.(\w+)\.?(.*)?/)[0][0]
  get_extension_and_name_from_file(@template_path).merge(
    template_extension: template_extension
  )
end