From 71cd612878dcd17338de36810c9cf47e46f661d2 Mon Sep 17 00:00:00 2001 From: Dani Palou Date: Tue, 16 Jul 2019 16:40:41 +0200 Subject: [PATCH] MOBILE-3073 login: Display debug messages when adding site --- .../login/pages/site-error/site-error.html | 2 +- src/core/login/pages/site/site.ts | 10 +- src/providers/sites.ts | 25 +++-- src/providers/utils/dom.ts | 106 ++++++++++-------- 4 files changed, 87 insertions(+), 56 deletions(-) diff --git a/src/core/login/pages/site-error/site-error.html b/src/core/login/pages/site-error/site-error.html index 110a93a72..da3c478fe 100644 --- a/src/core/login/pages/site-error/site-error.html +++ b/src/core/login/pages/site-error/site-error.html @@ -20,7 +20,7 @@

{{ 'core.login.contactyouradministratorissue' | translate:{$a: ''} }}

-

+

diff --git a/src/core/login/pages/site/site.ts b/src/core/login/pages/site/site.ts index c4bd86fda..8b1fcf600 100644 --- a/src/core/login/pages/site/site.ts +++ b/src/core/login/pages/site/site.ts @@ -154,10 +154,14 @@ export class CoreLoginSitePage { * Show an error that aims people to solve the issue. * * @param {string} url The URL the user was trying to connect to. - * @param {string} error Error to display. + * @param {any} error Error to display. */ - protected showLoginIssue(url: string, error: string): void { - const modal = this.modalCtrl.create('CoreLoginSiteErrorPage', { siteUrl: url, issue: error }); + protected showLoginIssue(url: string, error: any): void { + const modal = this.modalCtrl.create('CoreLoginSiteErrorPage', { + siteUrl: url, + issue: this.domUtils.getErrorMessage(error) + }); + modal.present(); } } diff --git a/src/providers/sites.ts b/src/providers/sites.ts index b64da98fd..b01a412d1 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -363,7 +363,7 @@ export class CoreSitesProvider { return this.checkSiteWithProtocol(siteUrl, protocol).catch((error) => { // Do not continue checking if a critical error happened. if (error.critical) { - return Promise.reject(error.error); + return Promise.reject(error); } // Retry with the other protocol. @@ -371,13 +371,17 @@ export class CoreSitesProvider { return this.checkSiteWithProtocol(siteUrl, protocol).catch((secondError) => { if (secondError.critical) { - return Promise.reject(secondError.error); + return Promise.reject(secondError); } // Site doesn't exist. Return the error message. - return Promise.reject(this.textUtils.getErrorMessageFromError(error) || - this.textUtils.getErrorMessageFromError(secondError) || - this.translate.instant('core.cannotconnect')); + if (this.textUtils.getErrorMessageFromError(error)) { + return Promise.reject(error); + } else if (this.textUtils.getErrorMessageFromError(secondError)) { + return Promise.reject(secondError); + } else { + return this.translate.instant('core.cannotconnect'); + } }); }); } @@ -415,8 +419,11 @@ export class CoreSitesProvider { } // Return the error message. - return Promise.reject(this.textUtils.getErrorMessageFromError(error) || - this.textUtils.getErrorMessageFromError(secondError)); + if (this.textUtils.getErrorMessageFromError(error)) { + return Promise.reject(error); + } else { + return Promise.reject(secondError); + } }); }).then(() => { // Create a temporary site to check if local_mobile is installed. @@ -456,7 +463,9 @@ export class CoreSitesProvider { // Error, check if not supported. if (error.available === 1) { // Service supported but an error happened. Return error. - return Promise.reject({ error: error.error }); + error.critical = true; + + return Promise.reject(error); } return data; diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index 4f4fdab17..24374f3cd 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -598,6 +598,64 @@ export class CoreDomUtilsProvider { return this.textUtils.decodeHTML(this.translate.instant('core.error')); } + /** + * Get the error message from an error, including debug data if needed. + * + * @param {any} error Message to show. + * @param {boolean} [needsTranslate] Whether the error needs to be translated. + * @return {string} Error message, null if no error should be displayed. + */ + getErrorMessage(error: any, needsTranslate?: boolean): string { + let extraInfo = ''; + + if (typeof error == 'object') { + if (this.debugDisplay) { + // Get the debug info. Escape the HTML so it is displayed as it is in the view. + if (error.debuginfo) { + extraInfo = '

' + this.textUtils.escapeHTML(error.debuginfo); + } + if (error.backtrace) { + extraInfo += '

' + this.textUtils.replaceNewLines(this.textUtils.escapeHTML(error.backtrace), '
'); + } + + // tslint:disable-next-line + console.error(error); + } + + // 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. + return null; + } + + error = this.textUtils.getErrorMessageFromError(error); + if (!error) { + // 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. + const matches = error.match(/token"?[=|:]"?(\w*)/, ''); + if (matches && matches[1]) { + error = error.replace(new RegExp(matches[1], 'g'), 'secret'); + } + } + + if (error == CoreConstants.DONT_SHOW_ERROR) { + // The error shouldn't be shown, stop. + return null; + } + + let message = this.textUtils.decodeHTML(needsTranslate ? this.translate.instant(error) : error); + + if (extraInfo) { + message += extraInfo; + } + + return message; + } + /** * Retrieve component/directive instance. * Please use this function only if you cannot retrieve the instance using parent/child methods: ViewChild (or similar) @@ -1138,51 +1196,11 @@ export class CoreDomUtilsProvider { * @return {Promise} Promise resolved with the alert modal. */ showErrorModal(error: any, needsTranslate?: boolean, autocloseTime?: number): Promise { - let extraInfo = ''; + const message = this.getErrorMessage(error, needsTranslate); - if (typeof error == 'object') { - if (this.debugDisplay) { - // Get the debug info. Escape the HTML so it is displayed as it is in the view. - if (error.debuginfo) { - extraInfo = '

' + this.textUtils.escapeHTML(error.debuginfo); - } - if (error.backtrace) { - extraInfo += '

' + this.textUtils.replaceNewLines(this.textUtils.escapeHTML(error.backtrace), '
'); - } - - // tslint:disable-next-line - console.error(error); - } - - // 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. - return; - } - - error = this.textUtils.getErrorMessageFromError(error); - if (!error) { - // 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. - const matches = error.match(/token"?[=|:]"?(\w*)/, ''); - if (matches && matches[1]) { - error = error.replace(new RegExp(matches[1], 'g'), 'secret'); - } - } - - if (error == CoreConstants.DONT_SHOW_ERROR) { - // The error shouldn't be shown, stop. - return; - } - - let message = this.textUtils.decodeHTML(needsTranslate ? this.translate.instant(error) : error); - - if (extraInfo) { - message += extraInfo; + if (message === null) { + // Message doesn't need to be displayed, stop. + return Promise.resolve(null); } return this.showAlert(this.getErrorTitle(message), message, undefined, autocloseTime);