MOBILE-4059 core: Contact support from ajax errors

main
Noel De Martin 2022-10-06 16:13:28 +02:00
parent c34db4dcaf
commit dca3f76296
2 changed files with 17 additions and 6 deletions

View File

@ -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;
}

View File

@ -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);
}