Skip to content

Commit

Permalink
Replace throws with rejects to not lose errors
Browse files Browse the repository at this point in the history
Of course we still get shallowed exceptions coming from up operations we do that are erroneous and not direct throws. :/
  • Loading branch information
TomasHubelbauer committed Oct 13, 2024
1 parent e2ad8c8 commit ec3e311
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,21 +105,24 @@ export default class DOMParser {

return new Promise<Document>((resolve, reject) => {
const document = new Document();

// Beware of `HTMLRewriter` silently swallowing errors!
// See https://github.com/oven-sh/bun/issues/6124
new HTMLRewriter()
.on('*', {
element(element) {
// TODO: Carry over the HTML element attributes
if (element.tagName === 'HTML' || element.tagName === 'html') {
if (document.activeElement) {
throw new Error('Only one HTML element is allowed');
reject(new Error('Only one HTML element is allowed'));
}

return;
}

if (element.tagName === 'HEAD' || element.tagName === 'head') {
if (document.head) {
throw new Error('Only one HEAD element is allowed');
reject(new Error('Only one HEAD element is allowed'));
}

document.head = document.createElement('head');
Expand All @@ -129,7 +132,7 @@ export default class DOMParser {

if (element.tagName === 'BODY' || element.tagName === 'body') {
if (document.body) {
throw new Error('Only one BODY element is allowed');
reject(new Error('Only one BODY element is allowed'));
}

document.body = document.createElement('body');
Expand Down

0 comments on commit ec3e311

Please sign in to comment.