MOBILE-3859 ws: Improve error message for certificate errors

main
Dani Palou 2021-09-14 15:18:49 +02:00
parent 83ff9efbd8
commit 534bbbb289
6 changed files with 80 additions and 46 deletions

View File

@ -1384,6 +1384,7 @@
"core.add": "moodle", "core.add": "moodle",
"core.agelocationverification": "moodle", "core.agelocationverification": "moodle",
"core.ago": "message", "core.ago": "message",
"core.ajaxendpointnotfound": "local_moodlemobileapp",
"core.all": "moodle", "core.all": "moodle",
"core.allgroups": "moodle", "core.allgroups": "moodle",
"core.allparticipants": "moodle", "core.allparticipants": "moodle",
@ -1405,6 +1406,7 @@
"core.captureimage": "local_moodlemobileapp", "core.captureimage": "local_moodlemobileapp",
"core.capturevideo": "local_moodlemobileapp", "core.capturevideo": "local_moodlemobileapp",
"core.category": "moodle", "core.category": "moodle",
"core.certificaterror": "local_moodlemobileapp",
"core.choose": "moodle", "core.choose": "moodle",
"core.choosedots": "moodle", "core.choosedots": "moodle",
"core.clearsearch": "local_moodlemobileapp", "core.clearsearch": "local_moodlemobileapp",

View File

@ -19,12 +19,10 @@ import { CoreError } from '@classes/errors/error';
*/ */
export class CoreAjaxError extends CoreError { export class CoreAjaxError extends CoreError {
available?: number; // Whether the AJAX call is available. 0 if unknown, 1 if available, -1 if not available. available = 1; // @deprecated since app 4.0. AJAX endpoint should always be available in supported Moodle versions.
constructor(message: string, available?: number) { constructor(message: string) {
super(message); super(message);
this.available = typeof available == 'undefined' ? 0 : available;
} }
} }

View File

@ -1333,38 +1333,38 @@ export class CoreSite {
siteUrl: this.siteUrl, siteUrl: this.siteUrl,
}; };
let config: CoreSitePublicConfigResponse | undefined; let config: CoreSitePublicConfigResponse;
try { try {
config = await CoreWS.callAjax('tool_mobile_get_public_config', {}, preSets); config = await CoreWS.callAjax<CoreSitePublicConfigResponse>('tool_mobile_get_public_config', {}, preSets);
} catch (error) { } catch (error) {
if ((!this.getInfo() || this.isVersionGreaterEqualThan('3.8')) && error && error.errorcode == 'codingerror') { if (!error || error.errorcode !== 'codingerror' || (this.getInfo() && !this.isVersionGreaterEqualThan('3.8'))) {
// This error probably means that there is a redirect in the site. Try to use a GET request. throw error;
preSets.noLogin = true;
preSets.useGet = true;
try {
config = await CoreWS.callAjax('tool_mobile_get_public_config', {}, preSets);
} catch (error2) {
if (this.getInfo() && this.isVersionGreaterEqualThan('3.8')) {
// GET is supported, return the second error.
throw error2;
} else {
// GET not supported or we don't know if it's supported. Return first error.
throw error;
}
}
} }
throw error; // This error probably means that there is a redirect in the site. Try to use a GET request.
preSets.noLogin = true;
preSets.useGet = true;
try {
config = await CoreWS.callAjax<CoreSitePublicConfigResponse>('tool_mobile_get_public_config', {}, preSets);
} catch (error2) {
if (this.getInfo() && this.isVersionGreaterEqualThan('3.8')) {
// GET is supported, return the second error.
throw error2;
} else {
// GET not supported or we don't know if it's supported. Return first error.
throw error;
}
}
} }
// Use the wwwroot returned by the server. // Use the wwwroot returned by the server.
if (config!.httpswwwroot) { if (config.httpswwwroot) {
this.siteUrl = CoreUrlUtils.removeUrlParams(config!.httpswwwroot); // Make sure the URL doesn't have params. this.siteUrl = CoreUrlUtils.removeUrlParams(config.httpswwwroot); // Make sure the URL doesn't have params.
} }
return config!; return config;
} }
/** /**

View File

@ -3,6 +3,7 @@
"add": "Add", "add": "Add",
"agelocationverification": "Age and location verification", "agelocationverification": "Age and location verification",
"ago": "{{$a}} ago", "ago": "{{$a}} ago",
"ajaxendpointnotfound": "<p>AJAX endpoint not found. This can happen if the Moodle site is too old or it blocks access to this endpoint. The Moodle app only supports Moodle systems {{$a}} onwards. Please contact your site administrator.</p>\n<p>{{whoisadmin}}</p>",
"all": "All", "all": "All",
"allgroups": "All groups", "allgroups": "All groups",
"allparticipants": "All participants", "allparticipants": "All participants",
@ -23,6 +24,7 @@
"captureimage": "Take picture", "captureimage": "Take picture",
"capturevideo": "Record video", "capturevideo": "Record video",
"category": "Category", "category": "Category",
"certificaterror": "<p>The certificate of this site cannot be trusted by your device. Please contact your site administrator.</p>\n<p>{{whoisadmin}}</p>",
"choose": "Choose", "choose": "Choose",
"choosedots": "Choose...", "choosedots": "Choose...",
"clearsearch": "Clear search", "clearsearch": "Clear search",

View File

@ -245,27 +245,24 @@ export class CoreSitesProvider {
}); });
} }
} catch (error) { } catch (error) {
// Error, check if not supported. // Service supported but an error happened. Return error.
if (error.available === 1) { if (error.errorcode == 'codingerror') {
// Service supported but an error happened. Return error. // This could be caused by a redirect. Check if it's the case.
if (error.errorcode == 'codingerror') { const redirect = await CoreUtils.checkRedirect(siteUrl);
// This could be caused by a redirect. Check if it's the case.
const redirect = await CoreUtils.checkRedirect(siteUrl);
if (redirect) { if (redirect) {
error.error = Translate.instant('core.login.sitehasredirect'); error.message = Translate.instant('core.login.sitehasredirect');
} else { } else {
// We can't be sure if there is a redirect or not. Display cannot connect error. // We can't be sure if there is a redirect or not. Display cannot connect error.
error.error = Translate.instant('core.cannotconnecttrouble'); error.message = Translate.instant('core.cannotconnecttrouble');
}
} }
throw new CoreSiteError({
message: error.error,
errorcode: error.errorcode,
critical: true,
});
} }
throw new CoreSiteError({
message: error.message,
errorcode: error.errorcode,
critical: true,
});
} }
siteUrl = temporarySite.getURL(); siteUrl = temporarySite.getURL();

View File

@ -37,6 +37,7 @@ import { CoreWSError } from '@classes/errors/wserror';
import { CoreAjaxError } from '@classes/errors/ajaxerror'; import { CoreAjaxError } from '@classes/errors/ajaxerror';
import { CoreAjaxWSError } from '@classes/errors/ajaxwserror'; import { CoreAjaxWSError } from '@classes/errors/ajaxwserror';
import { CoreNetworkError } from '@classes/errors/network-error'; import { CoreNetworkError } from '@classes/errors/network-error';
import { CoreSite } from '@classes/site';
/** /**
* This service allows performing WS calls and download/upload files. * This service allows performing WS calls and download/upload files.
@ -474,9 +475,23 @@ export class CoreWSProvider {
return data.data; return data.data;
}, (data) => { }, (data) => {
const available = data.status == 404 ? -1 : 0; let message = '';
throw new CoreAjaxError(Translate.instant('core.serverconnection'), available); switch (data.status) {
case -2: // Certificate error.
message = this.getCertificateErrorMessage(data.error);
break;
case 404: // AJAX endpoint not found.
message = Translate.instant('core.ajaxendpointnotfound', {
$a: CoreSite.MINIMUM_MOODLE_VERSION,
whoisadmin: Translate.instant('core.whoissiteadmin'),
});
break;
default:
message = Translate.instant('core.serverconnection');
}
throw new CoreAjaxError(message);
}); });
} }
@ -675,12 +690,32 @@ export class CoreWSProvider {
} }
return retryPromise; return retryPromise;
} else if (error.status === -2) {
throw new CoreError(this.getCertificateErrorMessage(error.error));
} }
throw new CoreError(Translate.instant('core.serverconnection')); throw new CoreError(Translate.instant('core.serverconnection'));
}); });
} }
/**
* Get error message about certificate error.
*
* @param error Exact error message.
* @return Certificate error message.
*/
protected getCertificateErrorMessage(error?: string): string {
const message = Translate.instant('core.certificaterror', {
whoisadmin: Translate.instant('core.whoissiteadmin'),
});
if (error) {
return `${message}\n<p>${error}</p>`;
}
return message;
}
/** /**
* Retry all requests in the queue. * Retry all requests in the queue.
* This function uses recursion in order to add a delay between requests to reduce stress. * This function uses recursion in order to add a delay between requests to reduce stress.