Class: Statsig::ErrorBoundary
- Inherits:
-
Object
- Object
- Statsig::ErrorBoundary
- Defined in:
- lib/error_boundary.rb
Instance Method Summary collapse
- #capture(recover: -> {}, caller: nil) ⇒ Object
-
#initialize(sdk_key, local_mode = false) ⇒ ErrorBoundary
constructor
A new instance of ErrorBoundary.
- #log_exception(exception, tag: nil, extra: {}, force: false) ⇒ Object
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. 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 = Statsig. http = HTTP.headers( { 'STATSIG-API-KEY' => @sdk_key, 'STATSIG-SDK-TYPE' => ['sdkType'], 'STATSIG-SDK-VERSION' => ['sdkVersion'], 'STATSIG-SDK-LANGUAGE-VERSION' => ['languageVersion'], 'Content-Type' => 'application/json; charset=UTF-8' }).accept(:json) body = { 'exception' => name, 'info' => { 'trace' => exception.backtrace.to_s, 'message' => exception. }.to_s, 'statsigMetadata' => , 'tag' => tag, 'extra' => extra } http.post($endpoint, body: JSON.generate(body)) rescue StandardError return end |