Skip to content

Commit 3aea77a

Browse files
committed
MOBILE-3332 course: Display more data in errordownloadingsomefiles
1 parent 9dcd84c commit 3aea77a

File tree

7 files changed

+77
-12
lines changed

7 files changed

+77
-12
lines changed

src/addon/mod/book/components/index/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComp
119119
protected fetchContent(refresh?: boolean): Promise<any> {
120120
const promises = [];
121121
let downloadFailed = false;
122+
let downloadFailError;
122123

123124
// Try to get the book data.
124125
promises.push(this.bookProvider.getBook(this.courseId, this.module.id).then((book) => {
@@ -129,9 +130,10 @@ export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComp
129130
}));
130131

131132
// Download content. This function also loads module contents if needed.
132-
promises.push(this.prefetchDelegate.download(this.module, this.courseId).catch(() => {
133+
promises.push(this.prefetchDelegate.download(this.module, this.courseId).catch((error) => {
133134
// Mark download as failed but go on since the main files could have been downloaded.
134135
downloadFailed = true;
136+
downloadFailError = error;
135137

136138
if (!this.module.contents.length) {
137139
// Try to load module contents for offline usage.
@@ -163,7 +165,7 @@ export class AddonModBookIndexComponent extends CoreCourseModuleMainResourceComp
163165
return this.loadChapter(this.currentChapter).then(() => {
164166
if (downloadFailed && this.appProvider.isOnline()) {
165167
// We could load the main file but the download failed. Show error message.
166-
this.domUtils.showErrorModal('core.errordownloadingsomefiles', true);
168+
this.showErrorDownloadingSomeFiles(downloadFailError);
167169
}
168170
}).catch(() => {
169171
// Ignore errors, they're handled inside the loadChapter function.

src/addon/mod/imscp/components/index/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,18 @@ export class AddonModImscpIndexComponent extends CoreCourseModuleMainResourceCom
7676
*/
7777
protected fetchContent(refresh?: boolean): Promise<any> {
7878
let downloadFailed = false;
79+
let downloadFailError;
7980
const promises = [];
8081

8182
promises.push(this.imscpProvider.getImscp(this.courseId, this.module.id).then((imscp) => {
8283
this.description = imscp.intro;
8384
this.dataRetrieved.emit(imscp);
8485
}));
8586

86-
promises.push(this.imscpPrefetch.download(this.module, this.courseId).catch(() => {
87+
promises.push(this.imscpPrefetch.download(this.module, this.courseId).catch((error) => {
8788
// Mark download as failed but go on since the main files could have been downloaded.
8889
downloadFailed = true;
90+
downloadFailError = error;
8991

9092
return this.courseProvider.loadModuleContents(this.module, this.courseId).catch((error) => {
9193
// Error getting module contents, fail.
@@ -109,7 +111,7 @@ export class AddonModImscpIndexComponent extends CoreCourseModuleMainResourceCom
109111
}).then(() => {
110112
if (downloadFailed && this.appProvider.isOnline()) {
111113
// We could load the main file but the download failed. Show error message.
112-
this.domUtils.showErrorModal('core.errordownloadingsomefiles', true);
114+
this.showErrorDownloadingSomeFiles(downloadFailError);
113115
}
114116

115117
}).finally(() => {

src/addon/mod/page/components/index/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,13 @@ export class AddonModPageIndexComponent extends CoreCourseModuleMainResourceComp
7878
*/
7979
protected fetchContent(refresh?: boolean): Promise<any> {
8080
let downloadFailed = false;
81+
let downloadFailError;
8182

8283
// Download content. This function also loads module contents if needed.
83-
return this.pagePrefetch.download(this.module, this.courseId).catch(() => {
84+
return this.pagePrefetch.download(this.module, this.courseId).catch((error) => {
8485
// Mark download as failed but go on since the main files could have been downloaded.
8586
downloadFailed = true;
87+
downloadFailError = error;
8688
}).then(() => {
8789
if (!this.module.contents.length) {
8890
// Try to load module contents for offline usage.
@@ -132,7 +134,7 @@ export class AddonModPageIndexComponent extends CoreCourseModuleMainResourceComp
132134

133135
if (downloadFailed && this.appProvider.isOnline()) {
134136
// We could load the main file but the download failed. Show error message.
135-
this.domUtils.showErrorModal('core.errordownloadingsomefiles', true);
137+
this.showErrorDownloadingSomeFiles(downloadFailError);
136138
}
137139
}));
138140

src/addon/mod/resource/components/index/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,12 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource
110110

111111
if (this.resourceHelper.isDisplayedInIframe(this.module)) {
112112
let downloadFailed = false;
113+
let downloadFailError;
113114

114-
return this.prefetchHandler.download(this.module, this.courseId).catch(() => {
115+
return this.prefetchHandler.download(this.module, this.courseId).catch((error) => {
115116
// Mark download as failed but go on since the main files could have been downloaded.
116117
downloadFailed = true;
118+
downloadFailError = error;
117119
}).then(() => {
118120
return this.resourceHelper.getIframeSrc(this.module).then((src) => {
119121
this.mode = 'iframe';
@@ -131,7 +133,7 @@ export class AddonModResourceIndexComponent extends CoreCourseModuleMainResource
131133

132134
if (downloadFailed && this.appProvider.isOnline()) {
133135
// We could load the main file but the download failed. Show error message.
134-
this.domUtils.showErrorModal('core.errordownloadingsomefiles', true);
136+
this.showErrorDownloadingSomeFiles(downloadFailError);
135137
}
136138
});
137139
});

src/core/course/classes/main-resource-component.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { NavController } from 'ionic-angular';
1717
import { TranslateService } from '@ngx-translate/core';
1818
import { CoreLoggerProvider } from '@providers/logger';
1919
import { CoreDomUtilsProvider } from '@providers/utils/dom';
20-
import { CoreTextUtilsProvider } from '@providers/utils/text';
20+
import { CoreTextUtilsProvider, CoreTextErrorObject } from '@providers/utils/text';
2121
import { CoreCourseHelperProvider } from '@core/course/providers/helper';
2222
import { CoreCourseModuleMainComponent, CoreCourseModuleDelegate } from '@core/course/providers/module-delegate';
2323
import { CoreCourseSectionPage } from '@core/course/pages/section/section.ts';
@@ -265,6 +265,20 @@ export class CoreCourseModuleMainResourceComponent implements OnInit, OnDestroy,
265265
this.courseHelper.confirmAndRemoveFiles(this.module, this.courseId);
266266
}
267267

268+
/**
269+
* Show an error occurred while downloading files.
270+
*
271+
* @param error The specific error.
272+
*/
273+
protected showErrorDownloadingSomeFiles(error: string | CoreTextErrorObject): void {
274+
const errorMessage = this.textUtils.buildSeveralParagraphsMessage([
275+
this.translate.instant('core.errordownloadingsomefiles'),
276+
error,
277+
]);
278+
279+
this.domUtils.showErrorModal(errorMessage);
280+
}
281+
268282
/**
269283
* Component being destroyed.
270284
*/

src/core/courses/providers/course-link-handler.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,9 @@ export class CoreCoursesCourseLinkHandler extends CoreContentLinksHandlerBase {
178178
error = this.translate.instant('core.courses.notenroled');
179179
}
180180

181-
const body = this.translate.instant('core.twoparagraphs',
182-
{ p1: error, p2: this.translate.instant('core.confirmopeninbrowser') });
181+
const body = this.textUtils.buildSeveralParagraphsMessage(
182+
[error, this.translate.instant('core.confirmopeninbrowser')]);
183+
183184
this.domUtils.showConfirm(body).then(() => {
184185
this.sitesProvider.getCurrentSite().openInBrowserWithAutoLogin(url);
185186
}).catch(() => {

src/providers/utils/text.ts

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@ import { ModalController, Platform } from 'ionic-angular';
1818
import { TranslateService } from '@ngx-translate/core';
1919
import { CoreLangProvider } from '../lang';
2020

21+
/**
22+
* Different type of errors the app can treat.
23+
*/
24+
export type CoreTextErrorObject = {
25+
message?: string;
26+
error?: string;
27+
content?: string;
28+
body?: string;
29+
};
30+
2131
/*
2232
* "Utils" service with helper functions for text.
2333
*/
@@ -122,6 +132,38 @@ export class CoreTextUtilsProvider {
122132
return result;
123133
}
124134

135+
/**
136+
* Build a message with several paragraphs.
137+
*
138+
* @param paragraphs List of paragraphs.
139+
* @return Built message.
140+
*/
141+
buildSeveralParagraphsMessage(paragraphs: (string | CoreTextErrorObject)[]): string {
142+
// Filter invalid messages, and convert them to messages in case they're errors.
143+
const messages: string[] = [];
144+
145+
paragraphs.forEach((paragraph) => {
146+
// If it's an error, get its message.
147+
const message = this.getErrorMessageFromError(paragraph);
148+
149+
if (paragraph) {
150+
messages.push(message);
151+
}
152+
});
153+
154+
if (messages.length < 2) {
155+
return messages[0] || '';
156+
}
157+
158+
let builtMessage = messages[0];
159+
160+
for (let i = 1; i < messages.length; i++) {
161+
builtMessage = this.translate.instant('core.twoparagraphs', { p1: builtMessage, p2: messages[i] });
162+
}
163+
164+
return builtMessage;
165+
}
166+
125167
/**
126168
* Convert size in bytes into human readable format
127169
*
@@ -449,7 +491,7 @@ export class CoreTextUtilsProvider {
449491
* @param error Error object.
450492
* @return Error message, undefined if not found.
451493
*/
452-
getErrorMessageFromError(error: any): string {
494+
getErrorMessageFromError(error: string | CoreTextErrorObject): string {
453495
if (typeof error == 'string') {
454496
return error;
455497
}

0 commit comments

Comments
 (0)