Module: Exceptional::Api

Included in:
Exceptional
Defined in:
lib/exceptional/api.rb

Instance Method Summary collapse

Instance Method Details

#catch(exception) ⇒ Object



65
66
67
68
69
# File 'lib/exceptional/api.rb', line 65

def catch(exception)
  exception_data = parse(exception)
  exception_data.controller_name = File.basename($0)
  post(exception_data)
end

#handle(exception, controller, request, params) ⇒ Object

used with Rails, takes an exception, controller, request and parameters creates an ExceptionData object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/exceptional/api.rb', line 29

def handle(exception, controller, request, params)
  Exceptional.log! "Handling #{exception.message}", 'info'
  begin
    e = parse(exception)
    # Additional data for Rails Exceptions
    e.framework = "rails"
    e.controller_name = controller.controller_name
    e.action_name = controller.action_name
    e.application_root = Exceptional.application_root
    e.occurred_at = Time.now.strftime("%Y%m%d %H:%M:%S %Z")
    e.environment = request.env.to_hash
    e.url = "#{request.protocol}#{request.host}#{request.request_uri}"
    e.environment = safe_environment(request)
    e.session = safe_session(request.session)
    e.parameters = sanitize_hash(params.to_hash)

    post(e)
  rescue Exception => exception
    Exceptional.log! "Error preparing exception data."
    Exceptional.log! exception.message
    Exceptional.log! exception.backtrace.join("\n"), 'debug'
  end
end

#parse(exception) ⇒ Object

parse an exception into an ExceptionData object



8
9
10
11
12
13
14
# File 'lib/exceptional/api.rb', line 8

def parse(exception)
  exception_data = ExceptionData.new
  exception_data.exception_backtrace = exception.backtrace
  exception_data.exception_message = exception.message
  exception_data.exception_class = exception.class.to_s
  exception_data
end

#post(exception_data) ⇒ Object

post the given exception data to getexceptional.com



17
18
19
20
21
22
23
24
25
# File 'lib/exceptional/api.rb', line 17

def post(exception_data)
  hash = exception_data.to_hash
  if hash[:session]
    hash[:session].delete("initialization_options")
    hash[:session].delete("request")
  end

  Exceptional.post_exception(hash.to_json)
end

#rescue(&block) ⇒ Object

rescue any exceptions within the given block, send it to exceptional, then raise



56
57
58
59
60
61
62
63
# File 'lib/exceptional/api.rb', line 56

def rescue(&block)
  begin
    block.call
  rescue Exception => e
    self.catch(e)
    raise(e)
  end
end