Skip to content

Commit

Permalink
MOBILE-4608 core: Parse error message from HTML error pages
Browse files Browse the repository at this point in the history
  • Loading branch information
albertgasset committed Aug 14, 2024
1 parent 5c4da9a commit 7866b31
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion src/core/services/utils/text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<HTMLElement>('.errorbox .errormessage');

return element?.innerText.trim() ?? '';
}

/**
Expand Down

0 comments on commit 7866b31

Please sign in to comment.