From dca3f76296991142368133f69b820592a0ee5506 Mon Sep 17 00:00:00 2001 From: Noel De Martin Date: Thu, 6 Oct 2022 16:13:28 +0200 Subject: [PATCH] MOBILE-4059 core: Contact support from ajax errors --- src/core/classes/errors/ajaxerror.ts | 8 ++++---- src/core/services/ws.ts | 15 +++++++++++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/src/core/classes/errors/ajaxerror.ts b/src/core/classes/errors/ajaxerror.ts index 70f5eeee1..9761403c9 100644 --- a/src/core/classes/errors/ajaxerror.ts +++ b/src/core/classes/errors/ajaxerror.ts @@ -12,18 +12,18 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { CoreError } from '@classes/errors/error'; +import { CoreSiteError, CoreSiteErrorOptions } from '@classes/errors/siteerror'; /** * Generic error returned by an Ajax call. */ -export class CoreAjaxError extends CoreError { +export class CoreAjaxError extends CoreSiteError { available = 1; // @deprecated since app 4.0. AJAX endpoint should always be available in supported Moodle versions. status?: number; - constructor(message: string, available?: number, status?: number) { - super(message); + constructor(messageOrOptions: string | CoreSiteErrorOptions, available?: number, status?: number) { + super(typeof messageOrOptions === 'string' ? { message: messageOrOptions } : messageOrOptions); this.status = status; } diff --git a/src/core/services/ws.ts b/src/core/services/ws.ts index 4c96e8a6c..62f5815f9 100644 --- a/src/core/services/ws.ts +++ b/src/core/services/ws.ts @@ -39,6 +39,8 @@ import { CoreSite } from '@classes/site'; import { CoreHttpError } from '@classes/errors/httperror'; import { CorePromisedValue } from '@classes/promised-value'; import { CorePlatform } from '@services/platform'; +import { CoreUtils } from '@services/utils/utils'; +import { CoreSites } from '@services/sites'; /** * This service allows performing WS calls and download/upload files. @@ -456,7 +458,7 @@ export class CoreWSProvider { }); } - return promise.then((response) => { + return promise.then(async (response) => { let data = response.body; // Some moodle web services return null. @@ -467,7 +469,16 @@ 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') { - throw new CoreAjaxError(Translate.instant('core.serverconnection')); + const siteConfig = await CoreUtils.ignoreErrors(CoreSites.getPublicSiteConfigByUrl(preSets.siteUrl)); + + throw new CoreAjaxError({ + siteConfig, + contactSupport: true, + message: Translate.instant('core.cannotconnecttrouble'), + fallbackMessage: Translate.instant('core.cannotconnecttroublewithoutsupport'), + errorcode: 'invalidresponse', + errorDetails: Translate.instant('core.serverconnection'), + }); } else if (data.error) { throw new CoreAjaxWSError(data); }