From e7e48ee78a948dd9f7279df91cd27c1d315a4bae Mon Sep 17 00:00:00 2001 From: dpalou Date: Fri, 5 Oct 2018 15:35:59 +0200 Subject: [PATCH] MOBILE-2640 errors: Display backtrace for AJAX calls --- src/providers/utils/dom.ts | 13 +++++++++---- src/providers/ws.ts | 26 ++++++++++++-------------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index 40c9fca57..b64ec82fc 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -988,12 +988,17 @@ export class CoreDomUtilsProvider { * @return {Promise} Promise resolved with the alert modal. */ showErrorModal(error: any, needsTranslate?: boolean, autocloseTime?: number): Promise { - let extraInfo; + let extraInfo = ''; if (typeof error == 'object') { - if (this.debugDisplay && error.debuginfo) { + if (this.debugDisplay) { // Get the debug info. Escape the HTML so it is displayed as it is in the view. - extraInfo = this.textUtils.escapeHTML(error.debuginfo); + if (error.debuginfo) { + extraInfo = '

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

' + this.textUtils.replaceNewLines(this.textUtils.escapeHTML(error.backtrace), '
'); + } } // We received an object instead of a string. Search for common properties. @@ -1029,7 +1034,7 @@ export class CoreDomUtilsProvider { let message = this.textUtils.decodeHTML(needsTranslate ? this.translate.instant(error) : error); if (extraInfo) { - message += '

' + extraInfo; + message += extraInfo; } return this.showAlert(this.getErrorTitle(message), message, undefined, autocloseTime); diff --git a/src/providers/ws.ts b/src/providers/ws.ts index f6d6dd70c..84c85bd71 100644 --- a/src/providers/ws.ts +++ b/src/providers/ws.ts @@ -224,9 +224,9 @@ export class CoreWSProvider { ajaxData; if (typeof preSets.siteUrl == 'undefined') { - return rejectWithError(this.translate.instant('core.unexpectederror')); + return rejectWithError(this.createFakeWSError('core.unexpectederror', true)); } else if (!this.appProvider.isOnline()) { - return rejectWithError(this.translate.instant('core.networkerrormsg')); + return rejectWithError(this.createFakeWSError('core.networkerrormsg', true)); } if (typeof preSets.responseExpected == 'undefined') { @@ -252,40 +252,38 @@ export class CoreWSProvider { // Check if error. Ajax layer should always return an object (if error) or an array (if success). if (!data || typeof data != 'object') { - return rejectWithError(this.translate.instant('core.serverconnection')); + return rejectWithError(this.createFakeWSError('core.serverconnection', true)); } else if (data.error) { - return rejectWithError(data.error, data.errorcode); + return rejectWithError(data.error); } // Get the first response since only one request was done. data = data[0]; if (data.error) { - return rejectWithError(data.exception.message, data.exception.errorcode); + return rejectWithError(data.exception); } return data.data; }, (data) => { const available = data.status == 404 ? -1 : 0; - return rejectWithError(this.translate.instant('core.serverconnection'), '', available); + return rejectWithError(this.createFakeWSError('core.serverconnection', true), available); }); // Convenience function to return an error. - function rejectWithError(message: string, code?: string, available?: number): Promise { + function rejectWithError(exception: any, available?: number): Promise { if (typeof available == 'undefined') { - if (code) { - available = code == 'invalidrecord' ? -1 : 1; + if (exception.errorcode) { + available = exception.errorcode == 'invalidrecord' ? -1 : 1; } else { available = 0; } } - return Promise.reject({ - error: message, - errorcode: code, - available: available - }); + exception.available = available; + + return Promise.reject(exception); } }