Class: Utility::ExceptionTracking

Inherits:
Object
  • Object
show all
Defined in:
lib/utility/exception_tracking.rb

Class Method Summary collapse

Class Method Details

.augment_exception(exception) ⇒ Object



33
34
35
36
37
38
39
40
41
# File 'lib/utility/exception_tracking.rb', line 33

def augment_exception(exception)
  unless exception.respond_to?(:id)
    exception.instance_eval do
      def id
        @error_id ||= BSON::ObjectId.new.to_s
      end
    end
  end
end

.capture_exception(exception, context = {}) ⇒ Object



23
24
25
26
# File 'lib/utility/exception_tracking.rb', line 23

def capture_exception(exception, context = {})
  Utility::Logger.log_stacktrace(generate_stack_trace(exception))
  Utility::Logger.error("Context: #{context.inspect}") if context
end

.capture_message(message, context = {}) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/utility/exception_tracking.rb', line 15

def capture_message(message, context = {})
  Utility::Logger.error("Error: #{message}. Context: #{context.inspect}")

  # When the method is called from a rescue block, our return value may leak outside of its
  # intended scope, so let's explicitly return nil here to be safe.
  nil
end

.generate_error_message(exception, message, context) ⇒ Object



43
44
45
46
47
48
49
50
# File 'lib/utility/exception_tracking.rb', line 43

def generate_error_message(exception, message, context)
  context = { :message_id => exception.id }.merge(context || {}) if exception.respond_to?(:id)
  context_message = context && "Context: #{context.inspect}"
  ['Exception', message, exception.class.to_s, exception.message, context_message]
    .compact
    .map { |part| part.to_s.dup.force_encoding('UTF-8') }
    .join(': ')
end

.generate_stack_trace(exception) ⇒ Object



52
53
54
55
56
57
58
59
60
61
# File 'lib/utility/exception_tracking.rb', line 52

def generate_stack_trace(exception)
  full_message = exception.full_message

  cause = exception
  while cause.cause != cause && (cause = cause.cause)
    full_message << "Cause:\n#{cause.full_message}"
  end

  full_message.dup.force_encoding('UTF-8')
end

.log_exception(exception, message = nil) ⇒ Object



28
29
30
31
# File 'lib/utility/exception_tracking.rb', line 28

def log_exception(exception, message = nil)
  Utility::Logger.error(message) if message
  Utility::Logger.log_stacktrace(generate_stack_trace(exception))
end