From 3ad80f201f31276b7deb56a41d6f209bcd6bdff4 Mon Sep 17 00:00:00 2001 From: Christoph Reiter <reiter.christoph@gmail.com> Date: Tue, 17 Sep 2019 17:45:30 +0200 Subject: [PATCH] Add a wrapper for Sentry.showReportDialog() --- packages/common/errorreport.js | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/common/errorreport.js b/packages/common/errorreport.js index d1740793..c350db7d 100644 --- a/packages/common/errorreport.js +++ b/packages/common/errorreport.js @@ -2,6 +2,9 @@ import * as Sentry from '@sentry/browser'; import env from './env.js'; import environment from 'consts:environment'; +let _isInitialized = false +let _canReportEvent = false; + /** * Initializes error reporting. * @@ -16,6 +19,9 @@ export function init(options) { }; let actual = Object.assign({}, defaults, options); + if (_isInitialized) + throw new Error("Already initialized"); + let sentryOptions = {debug: actual.debug, environment: environment}; if (actual.release) { @@ -36,9 +42,32 @@ export function init(options) { }; } else { sentryOptions['dsn'] = env.sentryDSN; + _canReportEvent = true; } Sentry.init(sentryOptions); + + _isInitialized = true; +} + +/** + * Whether showReportDialog() will work. + */ +export function canReportEvent() { + if (!_isInitialized) + throw new Error("Not initialized"); + return _canReportEvent; +} + +/** + * Show a report dialog for user error feedback. + * + * Call canReportEvent() first to see if this will do anything. + */ +export function showReportDialog() { + if (!canReportEvent()) + return; + Sentry.showReportDialog(); } /** @@ -47,6 +76,8 @@ export function init(options) { * @param {*} exception */ export function captureException(exception) { + if (!_isInitialized) + throw new Error("Not initialized"); Sentry.captureException(exception); } @@ -57,6 +88,8 @@ export function captureException(exception) { * @param {String} [level=error] The loglevel (error, warning, info, debug) */ export function captureMessage(message, level) { + if (!_isInitialized) + throw new Error("Not initialized"); if (!level) level = 'error'; if (!['error', 'warning', 'info', 'debug'].includes(level)) -- GitLab