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 }