Module: ExceptionBrowser::Plugin

Defined in:
lib/exception_browser_plugin/rails/controller_extensions.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(target) ⇒ Object



4
5
6
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 4

def self.included(target)
  target.send(:extend,  ExceptionBrowser::Plugin::ClassMethods)
end

Instance Method Details

#backtrace_data(raw_lines) ⇒ Object



101
102
103
104
105
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 101

def backtrace_data(raw_lines)
  raw_lines = raw_lines.collect { |string| string.split("\n") }.flatten.collect(&:strip)
  raw_lines = raw_lines.reject  { |string| string.blank? or (string =~ /^(On line|\d+\:)/) }
  return      raw_lines.collect { |raw_line| backtrace_entry_data(raw_line) }
end

#backtrace_entry_data(raw_line) ⇒ Object



107
108
109
110
111
112
113
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 107

def backtrace_entry_data(raw_line)
  raw_line = self.class.sanitaize_backtrace_entry(raw_line).split(':')
  data = { :file => raw_line.shift,
           :line => raw_line.shift.to_i }
  data[:method] = raw_line.shift unless raw_line.empty?
  data
end

#environment_data(exception) ⇒ Object



115
116
117
118
119
120
121
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 115

def environment_data(exception)
  data = {
    "PROCESS" => $$,
    "SERVER"  => `hostname -s`.chomp }
  self.request.env.each { |k,v| data[k] = v }
  data
end

#exception_data(exception) ⇒ Object



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
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 71

def exception_data(exception)
  deliverer = self.class.exception_data
  data = case deliverer
    when nil    then {}
    when Symbol then send(deliverer)
    when Proc   then deliverer.call(self)
  end
  
  { :exception => {
    :class_name      => exception.class.name,
    :controller_name => controller_name,
    :action_name     => action_name,
    :message         => exception.message,
    :extra_message   => data,
    :guid            => Digest::SHA1.hexdigest(Time.new.to_f.to_s + "." + (rand(8999)+1000).to_s).upcase,
    :raised_at       => Time.new,
    :environment     => environment_data(exception),
    :request => {
      :protocol      => request.protocol,
      :host          => request.env["HTTP_HOST"],
      :uri           => request.request_uri,
      :method        => request.method.to_s,
      :format        => request.format.to_s,
      :parameters    => request.parameters.to_hash,
      :rails_root    => Pathname.new(RAILS_ROOT).cleanpath.to_s
    },
    :backtrace       => backtrace_data(exception.backtrace)
  } }.to_xml.sub(/^<hash>/, "").sub(/<\/hash>$/, "")
end

#log_exception(exception) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 123

def log_exception(exception)
  return if ExceptionBrowser.gateway.blank? or ExceptionBrowser.reporter_token.blank?
  
  url = URI.parse(ExceptionBrowser.gateway)
  
  req = Net::HTTP::Post.new(url.path)
  req.set_content_type "text/xml"
  req.basic_auth ExceptionBrowser.reporter_token, 'x'
  req.body = exception_data(exception)
  
  res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }
  res.error! unless res.is_a? Net::HTTPSuccess
end

#rescue_action_in_public(exception) ⇒ Object



8
9
10
11
12
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 8

def rescue_action_in_public(exception)
  status = response_code_for_rescue(exception)
  render_optional_error_file status
  log_exception(exception) if status != :not_found
end

#rescue_action_locally(exception) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/exception_browser_plugin/rails/controller_extensions.rb', line 14

def rescue_action_locally(exception)
  add_variables_to_assigns
  @template.instance_variable_set("@exception", exception)
  @template.instance_variable_set("@rescues_path", File.dirname(rescues_path("stub")))
  @template.send!(:assign_variables_from_controller)
  
  @template.instance_variable_set("@contents", @template.render_file(template_path_for_local_rescue(exception), false))
  
  response.content_type = Mime::HTML
  
  status = response_code_for_rescue(exception)
  render_for_file(rescues_path("layout"), status)
  log_exception(exception) if status != :not_found and !ExceptionBrowser.only_in_public
end