Class: Statsig::ErrorBoundary

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

Instance Method Summary collapse

Constructor Details

#initialize(sdk_key, local_mode = false) ⇒ ErrorBoundary

Returns a new instance of ErrorBoundary.



8
9
10
11
12
# File 'lib/error_boundary.rb', line 8

def initialize(sdk_key, local_mode = false)
  @sdk_key = sdk_key
  @seen = Set.new
  @local_mode = local_mode
end

Instance Method Details

#capture(recover: -> {}, caller: nil) ⇒ Object



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

def capture(recover: -> {}, caller: nil)
  begin
    res = yield
  rescue StandardError, SystemStackError => e
    if e.is_a?(Statsig::UninitializedError) || e.is_a?(Statsig::ValueError)
      raise e
    end

    puts '[Statsig]: An unexpected exception occurred.'
    puts e.message
    log_exception(e, tag: caller&.to_s)
    res = recover.call
  end
  return res
end

#log_exception(exception, tag: nil, extra: {}, force: false) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/error_boundary.rb', line 30

def log_exception(exception, tag: nil, extra: {}, force: false)
  if @local_mode
    return
  end
  name = exception.class.name
  if @seen.include?(name) && !force
    return
  end

  @seen << name
  meta = Statsig.
  http = HTTP.headers(
    {
      'STATSIG-API-KEY' => @sdk_key,
      'STATSIG-SDK-TYPE' => meta['sdkType'],
      'STATSIG-SDK-VERSION' => meta['sdkVersion'],
      'STATSIG-SDK-LANGUAGE-VERSION' => meta['languageVersion'],
      'Content-Type' => 'application/json; charset=UTF-8'
    }).accept(:json)
  body = {
    'exception' => name,
    'info' => {
      'trace' => exception.backtrace.to_s,
      'message' => exception.message
    }.to_s,
    'statsigMetadata' => meta,
    'tag' => tag,
    'extra' => extra
  }
  http.post($endpoint, body: JSON.generate(body))
rescue StandardError
  return
end