MOBILE-3780 ws: Improve error displayed when upload file

main
Dani Palou 2022-02-23 12:36:47 +01:00
parent 8b67def43c
commit 40a0d63394
2 changed files with 57 additions and 40 deletions

View File

@ -530,6 +530,18 @@ export class CoreTextUtilsProvider {
return error.message || error.error || error.content || error.body; return error.message || error.error || error.content || error.body;
} }
/**
* Given some HTML code, return the HTML code inside <body> tags. If there are no body tags, return the whole HTML.
*
* @param html HTML text.
* @return Body HTML.
*/
getHTMLBodyContent(html: string): string {
const matches = html.match(/<body>([\s\S]*)<\/body>/im);
return matches?.[1] ?? html;
}
/** /**
* Get the pluginfile URL to replace @@PLUGINFILE@@ wildcards. * Get the pluginfile URL to replace @@PLUGINFILE@@ wildcards.
* *

View File

@ -16,7 +16,7 @@ import { Injectable } from '@angular/core';
import { HttpResponse, HttpParams } from '@angular/common/http'; import { HttpResponse, HttpParams } from '@angular/common/http';
import { FileEntry } from '@ionic-native/file/ngx'; import { FileEntry } from '@ionic-native/file/ngx';
import { FileUploadOptions } from '@ionic-native/file-transfer/ngx'; import { FileUploadOptions, FileUploadResult } from '@ionic-native/file-transfer/ngx';
import { Md5 } from 'ts-md5/dist/md5'; import { Md5 } from 'ts-md5/dist/md5';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { timeout } from 'rxjs/operators'; import { timeout } from 'rxjs/operators';
@ -885,8 +885,18 @@ export class CoreWSProvider {
options.headers = {}; options.headers = {};
options['Connection'] = 'close'; options['Connection'] = 'close';
let success: FileUploadResult;
try { try {
const success = await transfer.upload(filePath, uploadUrl, options, true); success = await transfer.upload(filePath, uploadUrl, options, true);
} catch (error) {
this.logger.error('Error while uploading file', filePath, error);
throw new CoreError(CoreTextUtils.buildSeveralParagraphsMessage([
Translate.instant('core.cannotconnecttrouble'),
CoreTextUtils.getHTMLBodyContent(CoreTextUtils.getErrorMessageFromError(error) || ''),
]));
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any // eslint-disable-next-line @typescript-eslint/no-explicit-any
const data = CoreTextUtils.parseJSON<any>( const data = CoreTextUtils.parseJSON<any>(
@ -925,11 +935,6 @@ export class CoreWSProvider {
this.logger.debug('Successfully uploaded file', filePath); this.logger.debug('Successfully uploaded file', filePath);
return data[0]; return data[0];
} catch (error) {
this.logger.error('Error while uploading file', filePath, error);
throw new CoreError(Translate.instant('core.errorinvalidresponse'));
}
} }
/** /**