diff --git a/src/addon/mod/assign/providers/assign-sync.ts b/src/addon/mod/assign/providers/assign-sync.ts index ae518eb4b..368eba7de 100644 --- a/src/addon/mod/assign/providers/assign-sync.ts +++ b/src/addon/mod/assign/providers/assign-sync.ts @@ -308,7 +308,7 @@ export class AddonModAssignSyncProvider extends CoreSyncBaseProvider { }).catch((error) => { if (error && this.utils.isWebServiceError(error)) { // A WebService has thrown an error, this means it cannot be submitted. Discard the submission. - discardError = error.message || error.error || error.content || error.body; + discardError = this.textUtils.getErrorMessageFromError(error); } else { // Couldn't connect to server, reject. return Promise.reject(error); @@ -402,7 +402,7 @@ export class AddonModAssignSyncProvider extends CoreSyncBaseProvider { }).catch((error) => { if (error && this.utils.isWebServiceError(error)) { // The WebService has thrown an error, this means it cannot be submitted. Discard the offline data. - discardError = error.message || error.error || error.content || error.body; + discardError = this.textUtils.getErrorMessageFromError(error); } else { // Couldn't connect to server, reject. return Promise.reject(error); diff --git a/src/addon/mod/choice/providers/sync.ts b/src/addon/mod/choice/providers/sync.ts index 03d72e5cd..48efe6663 100644 --- a/src/addon/mod/choice/providers/sync.ts +++ b/src/addon/mod/choice/providers/sync.ts @@ -176,7 +176,7 @@ export class AddonModChoiceSyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: data.name, - error: error.error + error: this.textUtils.getErrorMessageFromError(error) })); }); } diff --git a/src/addon/mod/data/providers/sync.ts b/src/addon/mod/data/providers/sync.ts index e29f29ab6..37ac1e714 100644 --- a/src/addon/mod/data/providers/sync.ts +++ b/src/addon/mod/data/providers/sync.ts @@ -295,10 +295,10 @@ export class AddonModDataSyncProvider extends CoreSyncBaseProvider { promises.push(actionPromise.catch((error) => { if (error && error.wserror) { // The WebService has thrown an error, this means it cannot be performed. Discard. - discardError = error.error; + discardError = this.textUtils.getErrorMessageFromError(error); } else { // Couldn't connect to server, reject. - return Promise.reject(error && error.error); + return Promise.reject(error); } }).then(() => { // Delete the offline data. diff --git a/src/addon/mod/feedback/providers/sync.ts b/src/addon/mod/feedback/providers/sync.ts index 469090738..21d3b34f8 100644 --- a/src/addon/mod/feedback/providers/sync.ts +++ b/src/addon/mod/feedback/providers/sync.ts @@ -251,12 +251,12 @@ export class AddonModFeedbackSyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: feedback.name, - error: error.error + error: this.textUtils.getErrorMessageFromError(error) })); }); } else { // Couldn't connect to server, reject. - return Promise.reject(error && error.error); + return Promise.reject(error); } }); } diff --git a/src/addon/mod/forum/providers/sync.ts b/src/addon/mod/forum/providers/sync.ts index 64b95f369..127843475 100644 --- a/src/addon/mod/forum/providers/sync.ts +++ b/src/addon/mod/forum/providers/sync.ts @@ -227,7 +227,7 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: data.name, - error: error.error + error: this.textUtils.getErrorMessageFromError(error) })); }); } else { @@ -399,7 +399,7 @@ export class AddonModForumSyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: data.name, - error: error.error + error: this.textUtils.getErrorMessageFromError(error) })); }); } else { diff --git a/src/addon/mod/glossary/providers/sync.ts b/src/addon/mod/glossary/providers/sync.ts index c78e8920e..00350ef75 100644 --- a/src/addon/mod/glossary/providers/sync.ts +++ b/src/addon/mod/glossary/providers/sync.ts @@ -198,7 +198,7 @@ export class AddonModGlossarySyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: data.concept, - error: error.error + error: this.textUtils.getErrorMessageFromError(error) })); }); } else { diff --git a/src/addon/mod/lesson/providers/lesson-sync.ts b/src/addon/mod/lesson/providers/lesson-sync.ts index acefccfe8..5afb4f829 100644 --- a/src/addon/mod/lesson/providers/lesson-sync.ts +++ b/src/addon/mod/lesson/providers/lesson-sync.ts @@ -401,7 +401,7 @@ export class AddonModLessonSyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: lesson.name, - error: error + error: this.textUtils.getErrorMessageFromError(error) })); }); } else { @@ -485,7 +485,7 @@ export class AddonModLessonSyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: lesson.name, - error: error + error: this.textUtils.getErrorMessageFromError(error) })); }); } else { diff --git a/src/addon/mod/survey/providers/sync.ts b/src/addon/mod/survey/providers/sync.ts index 74b1f160d..ac3601d15 100644 --- a/src/addon/mod/survey/providers/sync.ts +++ b/src/addon/mod/survey/providers/sync.ts @@ -173,13 +173,13 @@ export class AddonModSurveySyncProvider extends CoreSyncBaseProvider { result.warnings.push(this.translate.instant('core.warningofflinedatadeleted', { component: this.componentTranslate, name: data.name, - error: error.error + error: this.textUtils.getErrorMessageFromError(error) })); }); } // Couldn't connect to server, reject. - return Promise.reject(error && error.error); + return Promise.reject(error); }); }).then(() => { if (courseId) { diff --git a/src/addon/mod/wiki/providers/wiki-sync.ts b/src/addon/mod/wiki/providers/wiki-sync.ts index e402174ae..a042a04c9 100644 --- a/src/addon/mod/wiki/providers/wiki-sync.ts +++ b/src/addon/mod/wiki/providers/wiki-sync.ts @@ -291,7 +291,7 @@ export class AddonModWikiSyncProvider extends CoreSyncBaseProvider { const warning = this.translate.instant('core.warningofflinedatadeleted', { component: this.translate.instant('addon.mod_wiki.wikipage'), name: page.title, - error: error + error: this.textUtils.getErrorMessageFromError(error) }); result.discarded.push({ diff --git a/src/addon/mod/workshop/providers/sync.ts b/src/addon/mod/workshop/providers/sync.ts index 050ead192..e0f37b92c 100644 --- a/src/addon/mod/workshop/providers/sync.ts +++ b/src/addon/mod/workshop/providers/sync.ts @@ -340,7 +340,7 @@ export class AddonModWorkshopSyncProvider extends CoreSyncBaseProvider { }).catch((error) => { if (error && this.utils.isWebServiceError(error)) { // The WebService has thrown an error, this means it cannot be performed. Discard. - discardError = error.message || error.error; + discardError = this.textUtils.getErrorMessageFromError(error); } else { // Couldn't connect to server, reject. return Promise.reject(error); @@ -421,7 +421,7 @@ export class AddonModWorkshopSyncProvider extends CoreSyncBaseProvider { }).catch((error) => { if (error && this.utils.isWebServiceError(error)) { // The WebService has thrown an error, this means it cannot be performed. Discard. - discardError = error.message || error.error; + discardError = this.textUtils.getErrorMessageFromError(error); } else { // Couldn't connect to server, reject. return Promise.reject(error); @@ -480,10 +480,10 @@ export class AddonModWorkshopSyncProvider extends CoreSyncBaseProvider { evaluate.gradeover, siteId).catch((error) => { if (error && this.utils.isWebServiceError(error)) { // The WebService has thrown an error, this means it cannot be performed. Discard. - discardError = error.message || error.error; + discardError = this.textUtils.getErrorMessageFromError(error); } else { // Couldn't connect to server, reject. - return Promise.reject(error && error.error); + return Promise.reject(error); } }).then(() => { // Delete the offline data. @@ -539,10 +539,10 @@ export class AddonModWorkshopSyncProvider extends CoreSyncBaseProvider { evaluate.gradinggradeover, siteId).catch((error) => { if (error && this.utils.isWebServiceError(error)) { // The WebService has thrown an error, this means it cannot be performed. Discard. - discardError = error.message || error.error; + discardError = this.textUtils.getErrorMessageFromError(error); } else { // Couldn't connect to server, reject. - return Promise.reject(error && error.error); + return Promise.reject(error); } }).then(() => { // Delete the offline data. diff --git a/src/core/course/providers/sync.ts b/src/core/course/providers/sync.ts index 670040881..8a5334677 100644 --- a/src/core/course/providers/sync.ts +++ b/src/core/course/providers/sync.ts @@ -145,7 +145,7 @@ export class CoreCourseSyncProvider extends CoreSyncBaseProvider { // Responses deleted, add a warning. result.warnings.push(this.translate.instant('core.course.warningofflinemanualcompletiondeleted', { name: entry.coursename || courseId, - error: error.error + error: this.textUtils.getErrorMessageFromError(error) })); }); } diff --git a/src/core/courses/providers/course-link-handler.ts b/src/core/courses/providers/course-link-handler.ts index 4fa899f8f..092ac130d 100644 --- a/src/core/courses/providers/course-link-handler.ts +++ b/src/core/courses/providers/course-link-handler.ts @@ -16,6 +16,7 @@ import { Injectable } from '@angular/core'; import { TranslateService } from '@ngx-translate/core'; import { CoreSitesProvider } from '@providers/sites'; import { CoreDomUtilsProvider } from '@providers/utils/dom'; +import { CoreTextUtilsProvider } from '@providers/utils/text'; import { CoreContentLinksHandlerBase } from '@core/contentlinks/classes/base-handler'; import { CoreContentLinksAction } from '@core/contentlinks/providers/delegate'; import { CoreLoginHelperProvider } from '@core/login/providers/helper'; @@ -34,7 +35,8 @@ export class CoreCoursesCourseLinkHandler extends CoreContentLinksHandlerBase { constructor(private sitesProvider: CoreSitesProvider, private coursesProvider: CoreCoursesProvider, private loginHelper: CoreLoginHelperProvider, private domUtils: CoreDomUtilsProvider, - private translate: TranslateService, private courseProvider: CoreCourseProvider) { + private translate: TranslateService, private courseProvider: CoreCourseProvider, + private textUtils: CoreTextUtilsProvider) { super(); } @@ -150,7 +152,7 @@ export class CoreCoursesCourseLinkHandler extends CoreContentLinksHandlerBase { modal.dismiss(); if (error) { - error = error.message || error.error || error.content || error.body || error; + error = this.textUtils.getErrorMessageFromError(error) || error; } if (!error) { error = this.translate.instant('core.courses.notenroled'); @@ -232,7 +234,7 @@ export class CoreCoursesCourseLinkHandler extends CoreContentLinksHandlerBase { if (typeof password != 'undefined') { // The user attempted a password. Show an error message. - this.domUtils.showErrorModal(error.message); + this.domUtils.showErrorModal(error); } return this.domUtils.showPrompt(body, title, placeholder).then((password) => { diff --git a/src/core/login/pages/email-signup/email-signup.ts b/src/core/login/pages/email-signup/email-signup.ts index d48fdc609..314f38cb4 100644 --- a/src/core/login/pages/email-signup/email-signup.ts +++ b/src/core/login/pages/email-signup/email-signup.ts @@ -276,7 +276,7 @@ export class CoreLoginEmailSignupPage { } }); }).catch((error) => { - this.domUtils.showErrorModalDefault(error && error.error, 'core.login.usernotaddederror', true); + this.domUtils.showErrorModalDefault(error, 'core.login.usernotaddederror', true); }).finally(() => { modal.dismiss(); }); diff --git a/src/core/login/pages/forgotten-password/forgotten-password.ts b/src/core/login/pages/forgotten-password/forgotten-password.ts index 3aec2d353..2659c8126 100644 --- a/src/core/login/pages/forgotten-password/forgotten-password.ts +++ b/src/core/login/pages/forgotten-password/forgotten-password.ts @@ -70,7 +70,7 @@ export class CoreLoginForgottenPasswordPage { this.navCtrl.pop(); } }).catch((error) => { - this.domUtils.showErrorModal(error.error); + this.domUtils.showErrorModal(error); }).finally(() => { modal.dismiss(); }); diff --git a/src/core/login/pages/site-policy/site-policy.ts b/src/core/login/pages/site-policy/site-policy.ts index 30b56e58f..e97d54be4 100644 --- a/src/core/login/pages/site-policy/site-policy.ts +++ b/src/core/login/pages/site-policy/site-policy.ts @@ -88,7 +88,7 @@ export class CoreLoginSitePolicyPage { this.policyLoaded = true; }); }).catch((error) => { - this.domUtils.showErrorModalDefault(error && error.error, 'Error getting site policy.'); + this.domUtils.showErrorModalDefault(error, 'Error getting site policy.'); this.cancel(); }); } @@ -118,7 +118,7 @@ export class CoreLoginSitePolicyPage { return this.loginHelper.goToSiteInitialPage(); }); }).catch((error) => { - this.domUtils.showErrorModalDefault(error.message, 'Error accepting site policy.'); + this.domUtils.showErrorModalDefault(error, 'Error accepting site policy.'); }).finally(() => { modal.dismiss(); }); diff --git a/src/core/login/providers/helper.ts b/src/core/login/providers/helper.ts index a690bb6ec..079536ad1 100644 --- a/src/core/login/providers/helper.ts +++ b/src/core/login/providers/helper.ts @@ -1001,9 +1001,9 @@ export class CoreLoginHelperProvider { */ treatUserTokenError(siteUrl: string, error: any): void { if (error.errorcode == 'forcepasswordchangenotice') { - this.openChangePassword(siteUrl, error.error || error.message || error.body || error.content); + this.openChangePassword(siteUrl, this.textUtils.getErrorMessageFromError(error)); } else if (error.errorcode == 'legacymoodleversion') { - this.showLegacyNoticeModal(error.error); + this.showLegacyNoticeModal(this.textUtils.getErrorMessageFromError(error)); } else { this.domUtils.showErrorModal(error); } diff --git a/src/providers/sites.ts b/src/providers/sites.ts index 01495727e..8ceaffac4 100644 --- a/src/providers/sites.ts +++ b/src/providers/sites.ts @@ -388,7 +388,7 @@ export class CoreSitesProvider { const promise = this.http.post(siteUrl + '/login/token.php', data).timeout(CoreConstants.WS_TIMEOUT).toPromise(); return promise.catch((error) => { - return Promise.reject(error.message); + return Promise.reject(error); }).then((data: any) => { if (data.errorcode && (data.errorcode == 'enablewsdescription' || data.errorcode == 'requirecorrectaccess')) { return Promise.reject({ errorcode: data.errorcode, error: data.error }); diff --git a/src/providers/utils/dom.ts b/src/providers/utils/dom.ts index b64ec82fc..4ed2f7bee 100644 --- a/src/providers/utils/dom.ts +++ b/src/providers/utils/dom.ts @@ -1005,15 +1005,10 @@ export class CoreDomUtilsProvider { if (error.coreCanceled) { // It's a canceled error, don't display an error. return; - } else if (typeof error.content != 'undefined') { - error = error.content; - } else if (typeof error.body != 'undefined') { - error = error.body; - } else if (typeof error.message != 'undefined') { - error = error.message; - } else if (typeof error.error != 'undefined') { - error = error.error; - } else { + } + + 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. @@ -1058,7 +1053,7 @@ export class CoreDomUtilsProvider { let errorMessage = error; if (error && typeof error != 'string') { - errorMessage = error.message || error.error || error.content || error.body; + errorMessage = this.textUtils.getErrorMessageFromError(error); } return this.showErrorModal(typeof errorMessage == 'string' ? error : defaultError, needsTranslate, autocloseTime); diff --git a/src/providers/utils/text.ts b/src/providers/utils/text.ts index 3ee32224e..7ae4cecbf 100644 --- a/src/providers/utils/text.ts +++ b/src/providers/utils/text.ts @@ -400,6 +400,16 @@ export class CoreTextUtilsProvider { }); } + /** + * Get the error message from an error object. + * + * @param {any} error Error object. + * @return {string} Error message, undefined if not found. + */ + getErrorMessageFromError(error: any): string { + return error && (error.message || error.error || error.content || error.body); + } + /** * Get the pluginfile URL to replace @@PLUGINFILE@@ wildcards. *