2022-05-09 08:53:19 +00:00
|
|
|
###############
|
|
|
|
# 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)
|
|
|
|
@exception = exception
|
|
|
|
@user = user
|
|
|
|
@account = account
|
|
|
|
end
|
|
|
|
|
|
|
|
def capture_exception
|
2022-06-02 06:08:58 +00:00
|
|
|
if ENV['SENTRY_DSN'].present?
|
|
|
|
capture_exception_with_sentry
|
|
|
|
else
|
|
|
|
Rails.logger.error @exception
|
|
|
|
end
|
2022-05-09 08:53:19 +00:00
|
|
|
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
|
|
|
|
|
|
|
|
scope.set_user(id: @user.id, email: @user.email) if @user.is_a?(User)
|
|
|
|
Sentry.capture_exception(@exception)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|