Class: GLExceptionNotifier

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

Constant Summary collapse

CONTEXT_TYPES =
%i[extra_context tags_context user_context].freeze

Class Method Summary collapse

Class Method Details

.add_context(type, context) ⇒ Object

Raises:

  • (ArgumentError)


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/gl_exception_notifier.rb', line 43

def add_context(type, context)
  unless CONTEXT_TYPES.include?(type)
    raise ArgumentError,
          'type paramater must be one of: :extra_context, :tags_context, :user_context'
  end
  raise ArgumentError, 'contexts must be a hash' unless context.is_a?(Hash)

  case type
  when :user_context
    error_client.set_user(context)
  when :tags_context
    error_client.set_tags(context)
  when :extra_context
    error_client.set_extras(context)
  end
end

Raises:

  • (ArgumentError)


62
63
64
65
66
67
68
69
# File 'lib/gl_exception_notifier.rb', line 62

def breadcrumbs(data:, message: nil)
  raise ArgumentError, 'data must be a hash' unless data.is_a?(Hash)
  raise ArgumentError, 'when providing a message, it must be a string' if message && !message.is_a?(String)

  crumb = breadcrumb.new(message:, data:)
  error_client.add_breadcrumb(crumb)
  crumb
end

.call(*args) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/gl_exception_notifier.rb', line 7

def call(*args)
  if exceptionable?(args.first)
    capture_exception(args)
  else
    capture_message(args)
  end
end

.capture_exception(args) ⇒ Object



15
16
17
# File 'lib/gl_exception_notifier.rb', line 15

def capture_exception(args)
  error_client.capture_exception(*args)
end

.capture_message(args) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/gl_exception_notifier.rb', line 19

def capture_message(args)
  if args.first.is_a?(String)
    message = args.first
    extra = args.length == 2 && args.last.is_a?(Hash) ? args.last : { parameters: args.drop(1) }
  else
    message_info = if args.first.is_a?(Hash)
                     'called with kwargs, should have been positional'
                   else
                     'Unknown parameter set'
                   end
    message = "GLExceptionNotifier: #{message_info}"
    extra = { parameters: args }
  end

  error_client.capture_message(message, extra:)
end

.exceptionable?(obj) ⇒ Boolean



36
37
38
39
# File 'lib/gl_exception_notifier.rb', line 36

def exceptionable?(obj)
  obj.is_a?(Exception) ||
    (obj.respond_to?(:ancestors) && obj.ancestors.include?(Exception))
end

.last_breadcrumbObject



71
72
73
# File 'lib/gl_exception_notifier.rb', line 71

def last_breadcrumb
  error_client.get_current_scope&.breadcrumbs&.peek
end