From 5c4da9a5b1a382fac196d5dc633ffc64fca4832d Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Tue, 13 Aug 2024 15:23:40 +0200 Subject: [PATCH 1/2] MOBILE-4608 core: Fix FileTransferMock responses FileTransferMock was ignoring server response on errors, differing from the Cordova plugin. Also, it was not correctly returning response headers. Private function parseResponse has been removed as it is no longer needed. --- .../emulator/services/file-transfer.ts | 58 ++++++------------- 1 file changed, 17 insertions(+), 41 deletions(-) diff --git a/src/core/features/emulator/services/file-transfer.ts b/src/core/features/emulator/services/file-transfer.ts index 53a08957a..6c83037ad 100644 --- a/src/core/features/emulator/services/file-transfer.ts +++ b/src/core/features/emulator/services/file-transfer.ts @@ -12,7 +12,6 @@ // See the License for the specific language governing permissions and // limitations under the License. -import { CoreTextUtils } from '@services/utils/text'; import { CoreFile } from '@services/file'; /** @@ -117,25 +116,24 @@ export class FileTransferMock { }; xhr.onerror = (): void => { - const error = new FileTransferErrorMock(-1, source, target, xhr.status, xhr.statusText, ''); + const error = new FileTransferErrorMock(-1, source, target, xhr.status, '', ''); errorCallback(error); }; xhr.onload = async (): Promise => { // Finished dowloading the file. - let response = xhr.response || xhr.responseText; const status = Math.max(xhr.status === 1223 ? 204 : xhr.status, 0); if (status < 200 || status >= 300) { - // Request failed. Try to get the error message. - response = await this.parseResponse(response); - const error = new FileTransferErrorMock(-1, source, target, xhr.status, response || xhr.statusText, ''); + // Request failed. Try to get the respnse. + const body = xhr.response ? await this.blobToText(xhr.response) : ''; + const error = new FileTransferErrorMock(-1, source, target, xhr.status, body, ''); return errorCallback(error); } - if (!response) { - const error = new FileTransferErrorMock(-1, source, target, xhr.status, xhr.statusText, 'No response obtained'); + if (!xhr.response) { + const error = new FileTransferErrorMock(-1, source, target, xhr.status, '', 'No response obtained'); return errorCallback(error); } @@ -143,10 +141,16 @@ export class FileTransferMock { const basePath = CoreFile.getBasePathInstant(); target = target.replace(basePath, ''); // Remove basePath from the target. target = target.replace(/%20/g, ' '); // Replace all %20 with spaces. - CoreFile.writeFile(target, response) - .then(entry => - successCallback({ entry: entry as unknown as globalThis.FileEntry, headers: response.headers })) - .catch(error => errorCallback(error)); + + try { + const entry = await CoreFile.writeFile(target, xhr.response) ; + successCallback({ + entry: entry as unknown as globalThis.FileEntry, + headers: this.getHeadersAsObject(xhr), + }); + } catch (error) { + errorCallback(error); + } }; xhr.send(); @@ -214,34 +218,6 @@ export class FileTransferMock { return credentials && credentials[1]; } - /** - * Parse a response, converting it into text and the into an object if needed. - * - * @param response The response to parse. - * @returns Promise resolved with the parsed response. - */ - protected async parseResponse(response: Blob | ArrayBuffer | string | null): Promise { - if (!response) { - return ''; - - } - - let responseText = ''; - - if (response instanceof Blob) { - responseText = await this.blobToText(response); - - } else if (response instanceof ArrayBuffer) { - // Convert the ArrayBuffer into text. - responseText = String.fromCharCode.apply(null, new Uint8Array(response)); - - } else { - responseText = response; - } - - return CoreTextUtils.parseJSON(responseText, ''); - } - /** * Convert a Blob to text. * @@ -336,7 +312,7 @@ export class FileTransferMock { this.errorCallback = errorCallback; xhr.onerror = (): void => { - const error = new FileTransferErrorMock(-1, fileUrl, url, xhr.status, xhr.statusText, ''); + const error = new FileTransferErrorMock(-1, fileUrl, url, xhr.status, '', ''); errorCallback(error); }; From 7866b31a8fd7e9ef030ebc15d7dbfb32cc6af998 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Tue, 13 Aug 2024 15:37:22 +0200 Subject: [PATCH 2/2] MOBILE-4608 core: Parse error message from HTML error pages --- src/core/services/utils/text.ts | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/core/services/utils/text.ts b/src/core/services/utils/text.ts index 5cb5b553d..834394e74 100644 --- a/src/core/services/utils/text.ts +++ b/src/core/services/utils/text.ts @@ -515,7 +515,32 @@ export class CoreTextUtilsProvider { return undefined; } - return error.message || error.error || error.content || error.body; + if (error.message || error.error || error.content) { + return error.message || error.error || error.content; + } + + if (error.body) { + return this.getErrorMessageFromHTML(error.body); + } + + return undefined; + } + + /** + * Get the error message from an HTML error page. + * + * @param body HTML content. + * @returns Error message or empty string if not found. + */ + getErrorMessageFromHTML(body: string): string { + // THe parser does not throw errors and scripts are not executed. + const parser = new DOMParser(); + const doc = parser.parseFromString(body, 'text/html'); + + // Errors are rendered using the "errorbox" and "errormessage" classes since Moodle 2.0. + const element = doc.body.querySelector('.errorbox .errormessage'); + + return element?.innerText.trim() ?? ''; } /**