From 3a0c02ae0357c267881b30454b5320075378894b Mon Sep 17 00:00:00 2001 From: n4n5 <56606507+Its-Just-Nans@users.noreply.github.com> Date: Thu, 16 May 2024 12:52:35 +0200 Subject: [PATCH] add error message for svg without dimensions (#10924) * add svg * throw clean error * add error handling * revert * throw clean error * add changeset * make it a patch * Update remoteProbe.ts --------- Co-authored-by: Erika <3019731+Princesseuh@users.noreply.github.com> --- .changeset/cold-dolls-do.md | 5 ++++ packages/astro/src/assets/utils/metadata.ts | 30 ++++++++++++--------- 2 files changed, 23 insertions(+), 12 deletions(-) create mode 100644 .changeset/cold-dolls-do.md diff --git a/.changeset/cold-dolls-do.md b/.changeset/cold-dolls-do.md new file mode 100644 index 0000000000..4169415285 --- /dev/null +++ b/.changeset/cold-dolls-do.md @@ -0,0 +1,5 @@ +--- +"astro": patch +--- + +Handle image-size errors by displaying a clearer message diff --git a/packages/astro/src/assets/utils/metadata.ts b/packages/astro/src/assets/utils/metadata.ts index 953349eb52..8076d702fa 100644 --- a/packages/astro/src/assets/utils/metadata.ts +++ b/packages/astro/src/assets/utils/metadata.ts @@ -6,22 +6,28 @@ export async function imageMetadata( data: Uint8Array, src?: string ): Promise> { - const result = probe(data); + try { + const result = probe(data); + if (!result.height || !result.width || !result.type) { + throw new AstroError({ + ...AstroErrorData.NoImageMetadata, + message: AstroErrorData.NoImageMetadata.message(src), + }); + } - if (!result.height || !result.width || !result.type) { + const { width, height, type, orientation } = result; + const isPortrait = (orientation || 0) >= 5; + + return { + width: isPortrait ? height : width, + height: isPortrait ? width : height, + format: type as ImageInputFormat, + orientation, + }; + } catch (e) { throw new AstroError({ ...AstroErrorData.NoImageMetadata, message: AstroErrorData.NoImageMetadata.message(src), }); } - - const { width, height, type, orientation } = result; - const isPortrait = (orientation || 0) >= 5; - - return { - width: isPortrait ? height : width, - height: isPortrait ? width : height, - format: type as ImageInputFormat, - orientation, - }; }