From 7866b31a8fd7e9ef030ebc15d7dbfb32cc6af998 Mon Sep 17 00:00:00 2001 From: Albert Gasset Date: Tue, 13 Aug 2024 15:37:22 +0200 Subject: [PATCH] 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() ?? ''; } /**