Files
chatwoot/lib/chatwoot_exception_tracker.rb
Shivam Mishra 15d47f94a1 feat: support structured additional context in exception tracker
Allow callers to pass an additional_context hash that is attached to the
Sentry event as named context blocks via scope.set_context.
2026-06-23 12:02:00 +05:30

35 lines
1.1 KiB
Ruby

###############
# One library to capture_exception and send to the specific service.
# # e as exception, u for user and a for account (user and account are optional)
# Usage: ChatwootExceptionTracker(e, user: u, account: a).capture_exception
############
class ChatwootExceptionTracker
def initialize(exception, user: nil, account: nil, additional_context: {})
@exception = exception
@user = user
@account = account
@additional_context = additional_context
end
def capture_exception
capture_exception_with_sentry if ENV['SENTRY_DSN'].present?
Rails.logger.error @exception
end
private
def capture_exception_with_sentry
Sentry.with_scope do |scope|
if @account.present?
scope.set_context('account', { id: @account.id, name: @account.name })
scope.set_tags(account_id: @account.id)
end
@additional_context.each { |key, value| scope.set_context(key.to_s, value) }
scope.set_user(id: @user.id, email: @user.email) if @user.is_a?(User)
Sentry.capture_exception(@exception)
end
end
end