diff --git a/scripts/langindex.json b/scripts/langindex.json index f10a008d0..dc7ab8aa1 100644 --- a/scripts/langindex.json +++ b/scripts/langindex.json @@ -1467,6 +1467,8 @@ "core.settings.cordovadeviceuuid": "local_moodlemobileapp", "core.settings.cordovaversion": "local_moodlemobileapp", "core.settings.currentlanguage": "moodle", + "core.settings.debugdisplay": "admin", + "core.settings.debugdisplaydescription": "local_moodlemobileapp", "core.settings.deletesitefiles": "local_moodlemobileapp", "core.settings.deletesitefilestitle": "local_moodlemobileapp", "core.settings.deviceinfo": "local_moodlemobileapp", diff --git a/src/core/constants.ts b/src/core/constants.ts index ac25f07b6..7a21655f9 100644 --- a/src/core/constants.ts +++ b/src/core/constants.ts @@ -30,7 +30,8 @@ export class CoreConstants { static SETTINGS_RICH_TEXT_EDITOR = 'CoreSettingsRichTextEditor'; static SETTINGS_NOTIFICATION_SOUND = 'CoreSettingsNotificationSound'; static SETTINGS_SYNC_ONLY_ON_WIFI = 'CoreSettingsSyncOnlyOnWifi'; - static SETTINGS_REPORT_IN_BACKGROUND = 'CoreSettingsReportInBackground'; + static SETTINGS_DEBUG_DISPLAY = 'CoreSettingsDebugDisplay'; + static SETTINGS_REPORT_IN_BACKGROUND = 'CoreSettingsReportInBackground'; // @deprecated since 3.5.0 // WS constants. static WS_TIMEOUT = 30000; diff --git a/src/core/settings/lang/en.json b/src/core/settings/lang/en.json index 6e66023c2..1f9ebbfce 100644 --- a/src/core/settings/lang/en.json +++ b/src/core/settings/lang/en.json @@ -10,6 +10,8 @@ "cordovadeviceuuid": "Cordova device UUID", "cordovaversion": "Cordova version", "currentlanguage": "Current language", + "debugdisplay": "Display debug messages", + "debugdisplaydescription": "If enabled, error modals will display more data about the error if possible.", "deletesitefiles": "Are you sure that you want to delete the downloaded files from the site '{{sitename}}'?", "deletesitefilestitle": "Delete site files", "deviceinfo": "Device info", diff --git a/src/core/settings/pages/general/general.html b/src/core/settings/pages/general/general.html index dbfb02209..d313a9d76 100644 --- a/src/core/settings/pages/general/general.html +++ b/src/core/settings/pages/general/general.html @@ -17,8 +17,11 @@ - - {{ 'core.settings.reportinbackground' | translate }} - + + + {{ 'core.settings.debugdisplay' | translate }} + {{ 'core.settings.debugdisplaydescription' | translate }} + + diff --git a/src/core/settings/pages/general/general.ts b/src/core/settings/pages/general/general.ts index ab493b501..e6fbd9256 100644 --- a/src/core/settings/pages/general/general.ts +++ b/src/core/settings/pages/general/general.ts @@ -39,8 +39,7 @@ export class CoreSettingsGeneralPage { selectedLanguage: string; rteSupported: boolean; richTextEditor: boolean; - showReport: boolean; - reportInBackground: boolean; + debugDisplay: boolean; constructor(appProvider: CoreAppProvider, private configProvider: CoreConfigProvider, fileProvider: CoreFileProvider, private eventsProvider: CoreEventsProvider, private langProvider: CoreLangProvider, @@ -60,12 +59,9 @@ export class CoreSettingsGeneralPage { }); } - if (localStorage && localStorage.getItem && localStorage.setItem) { - this.showReport = true; - this.reportInBackground = parseInt(localStorage.getItem(CoreConstants.SETTINGS_REPORT_IN_BACKGROUND), 10) === 1; - } else { - this.showReport = false; - } + this.configProvider.get(CoreConstants.SETTINGS_DEBUG_DISPLAY, false).then((debugDisplay) => { + this.debugDisplay = !!debugDisplay; + }); } /** @@ -85,9 +81,10 @@ export class CoreSettingsGeneralPage { } /** - * Called when the report in background setting is enabled or disabled. + * Called when the debug display setting is enabled or disabled. */ - reportInBackgroundChanged(): void { - localStorage.setItem(CoreConstants.SETTINGS_REPORT_IN_BACKGROUND, this.reportInBackground ? '1' : '0'); + debugDisplayChanged(): void { + this.configProvider.set(CoreConstants.SETTINGS_DEBUG_DISPLAY, this.debugDisplay ? 1 : 0); + this.domUtils.setDebugDisplay(this.debugDisplay); } } diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index ccd28ddc3..40c9fca57 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -40,11 +40,18 @@ export class CoreDomUtilsProvider { protected matchesFn: string; // Name of the "matches" function to use when simulating a closest call. protected instances: {[id: string]: any} = {}; // Store component/directive instances by id. protected lastInstanceId = 0; + protected debugDisplay = false; // Whether to display debug messages. Store it in a variable to make it synchronous. constructor(private translate: TranslateService, private loadingCtrl: LoadingController, private toastCtrl: ToastController, private alertCtrl: AlertController, private textUtils: CoreTextUtilsProvider, private appProvider: CoreAppProvider, private platform: Platform, private configProvider: CoreConfigProvider, private urlUtils: CoreUrlUtilsProvider, - private modalCtrl: ModalController, private sanitizer: DomSanitizer) { } + private modalCtrl: ModalController, private sanitizer: DomSanitizer) { + + // Check if debug messages should be displayed. + configProvider.get(CoreConstants.SETTINGS_DEBUG_DISPLAY, false).then((debugDisplay) => { + this.debugDisplay = !!debugDisplay; + }); + } /** * Equivalent to element.closest(). If the browser doesn't support element.closest, it will @@ -841,6 +848,15 @@ export class CoreDomUtilsProvider { return this.scrollToElementBySelector(content, '.core-input-error', scrollParentClass); } + /** + * Set whether debug messages should be displayed. + * + * @param {boolean} value Whether to display or not. + */ + setDebugDisplay(value: boolean): void { + this.debugDisplay = value; + } + /** * Show an alert modal with a button to close it. * @@ -972,7 +988,14 @@ export class CoreDomUtilsProvider { * @return {Promise} Promise resolved with the alert modal. */ showErrorModal(error: any, needsTranslate?: boolean, autocloseTime?: number): Promise { + let extraInfo; + if (typeof error == 'object') { + if (this.debugDisplay && error.debuginfo) { + // Get the debug info. Escape the HTML so it is displayed as it is in the view. + extraInfo = this.textUtils.escapeHTML(error.debuginfo); + } + // We received an object instead of a string. Search for common properties. if (error.coreCanceled) { // It's a canceled error, don't display an error. @@ -988,6 +1011,7 @@ export class CoreDomUtilsProvider { } else { // No common properties found, just stringify it. error = JSON.stringify(error); + extraInfo = ''; // No need to add extra info because it's already in the error. } // Try to remove tokens from the contents. @@ -1002,7 +1026,11 @@ export class CoreDomUtilsProvider { return; } - const message = this.textUtils.decodeHTML(needsTranslate ? this.translate.instant(error) : error); + let message = this.textUtils.decodeHTML(needsTranslate ? this.translate.instant(error) : error); + + if (extraInfo) { + message += '' + extraInfo; + } return this.showAlert(this.getErrorTitle(message), message, undefined, autocloseTime); } @@ -1022,13 +1050,13 @@ export class CoreDomUtilsProvider { return; } + let errorMessage = error; + if (error && typeof error != 'string') { - error = error.message || error.error || error.content || error.body; + errorMessage = error.message || error.error || error.content || error.body; } - error = typeof error == 'string' ? error : defaultError; - - return this.showErrorModal(error, needsTranslate, autocloseTime); + return this.showErrorModal(typeof errorMessage == 'string' ? error : defaultError, needsTranslate, autocloseTime); } /**
{{ 'core.settings.debugdisplaydescription' | translate }}