From 3b6fd625fda5e0aa5f882ff9a8cb713b68de85ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Biro=C5=A1?= Date: Tue, 13 Feb 2024 15:03:02 +0100 Subject: [PATCH] fix: move custom onerror handler into options --- src/embed-images.ts | 19 ++++++++----------- src/types.ts | 4 ++++ 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/embed-images.ts b/src/embed-images.ts index e05186a5..6c261753 100644 --- a/src/embed-images.ts +++ b/src/embed-images.ts @@ -55,18 +55,15 @@ async function embedImageNode( const dataURL = await resourceToDataURL(url, getMimeType(url), options) await new Promise((resolve, reject) => { clonedNode.onload = resolve - clonedNode.onerror = async (...params) => { - if (clonedNode.onerror) { - try { - const result = await clonedNode.onerror(...params) - resolve(result) - } catch (error) { - reject(error) + clonedNode.onerror = options.onImageErrorHandler + ? (...attributes) => { + try { + resolve(options.onImageErrorHandler!(...attributes)) + } catch (error) { + reject(error) + } } - } else { - reject(new Error('Image failed to load')) - } - } + : reject const image = clonedNode as HTMLImageElement if (image.decode) { diff --git a/src/types.ts b/src/types.ts index b511363f..2546d196 100644 --- a/src/types.ts +++ b/src/types.ts @@ -91,4 +91,8 @@ export interface Options { * */ fetchRequestInit?: RequestInit + /** + * An event handler for the error event when any image in html has problem with loading. + */ + onImageErrorHandler?: OnErrorEventHandler }