Exception: Exception

Defined in:
lib/workety/extensions/exception.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.details(message = nil, details = {}) ⇒ Object

begin

raise StandardError.details("ALARM!", :a=>1)

rescue => ex

puts ex.view

end



29
30
31
32
33
34
35
36
37
# File 'lib/workety/extensions/exception.rb', line 29

def self.details message = nil, details = {}
  if message.kind_of? Hash
    message, details = nil, message
  end
  
  ex = new message
  ex.details.merge! details
  ex
end

Instance Method Details

#backtrace_viewObject



61
62
63
64
65
# File 'lib/workety/extensions/exception.rb', line 61

def backtrace_view
  ((bt = backtrace) && bt.collect{|line|"\t#{line}\n"}.join("") || "\tBacktrace undefined")
rescue ScriptError, StandardError => ex
  "\tERROR CREATING BACKTRACE VIEW: #{ex.summary_view}"
end

#detailsObject



19
20
21
# File 'lib/workety/extensions/exception.rb', line 19

def details
  @details ||= {}
end

#details_viewObject



53
54
55
56
57
58
59
# File 'lib/workety/extensions/exception.rb', line 53

def details_view
  "Details: #{details.inspect}" if details.any?
rescue ScriptError, StandardError => ex
  "Details: ERROR CREATING DETAILS VIEW:\n" \
  "#{ex.summary_view}\n" \
  "#{ex.backtrace_view}"
end

#log!Object



84
85
86
87
88
89
90
91
92
# File 'lib/workety/extensions/exception.rb', line 84

def log!
  logger.error view
  logger.flush if logger.respond_to? :flush
rescue ScriptError, StandardError => ex
  STDERR.write "ERROR: LOGGING ANOTHER EXCEPTION THE FOLLOWING EXCEPTION OCCURED:\n" \
               "#{ex.view}\n" \
               "THE FOLLOWING EXCEPTION WAS NOT STORED IN LOG:\n" \
               "#{view}\n"
end

#loggerObject

The following methods should not be called before Rails initialization.



70
71
72
# File 'lib/workety/extensions/exception.rb', line 70

def logger
  Rails.logger
end

#report!Object



74
75
76
77
# File 'lib/workety/extensions/exception.rb', line 74

def report!
  log!
  report_to_trackers! if Rails.env == "production"
end

#report_to_airbrake!Object

Airbrake API requires the following elements to be present:

/notice/error/class
/notice/error/backtrace/line
/notice/server-environment/environment-name


108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/workety/extensions/exception.rb', line 108

def report_to_airbrake!
  File.readable?(file = Rails.root + 'config' + 'airbrake.yml') &&
    (yaml = YAML.load_file file).kind_of?(Hash) &&
    (api_key = yaml["api-key"]).kind_of?(String) ||
    raise("Unable to read Airbrake api-key from #{file}")
  
  options = if details[:request].kind_of? Hash
    params = details.dup
    request = params.delete :request
    
    { url:           request[:url],
      component:     request[:controller],
      action:        request[:action],
      params:        (request[:params] || {}).merge(params),
      session:       request[:session],
      framework_env: Rails.env }
    
  else
    { params:        details,
      framework_env: Rails.env }
  end
  
  response = Toadhopper(api_key).post!(self, options)
  
  if response.status != 200
    raise StandardError.details("Tracker responded with status #{response.status}", body: response.body)
  end
  
rescue ScriptError, StandardError => ex
  ex.log!
end

#report_to_exceptional!Object



95
96
97
98
99
100
# File 'lib/workety/extensions/exception.rb', line 95

def report_to_exceptional!
  Exceptional::Remote.error Exceptional::DetailsExceptionData.new(self)
  Exceptional.context.clear!
rescue ScriptError, StandardError => ex
  ex.log!
end

#report_to_trackers!Object



79
80
81
82
# File 'lib/workety/extensions/exception.rb', line 79

def report_to_trackers!
  report_to_exceptional! if defined?(Exceptional)
  report_to_airbrake! if defined?(Toadhopper)
end

#summary_viewObject



49
50
51
# File 'lib/workety/extensions/exception.rb', line 49

def summary_view
  "#{self.class.name}: #{message}"
end

#viewObject



43
44
45
46
47
# File 'lib/workety/extensions/exception.rb', line 43

def view
  [summary_view, details_view, backtrace_view].compact.join("\n")
rescue ScriptError, StandardError
  "ERROR CREATING EXCEPTION VIEW"
end

#view!Object



39
40
41
# File 'lib/workety/extensions/exception.rb', line 39

def view!
  STDERR.write "#{view}\n"
end