Skip to content

Commit

Permalink
add error message for svg without dimensions (#10924)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
Its-Just-Nans and Princesseuh committed May 16, 2024
1 parent 303968b commit 3a0c02a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/cold-dolls-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": patch
---

Handle image-size errors by displaying a clearer message
30 changes: 18 additions & 12 deletions packages/astro/src/assets/utils/metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,28 @@ export async function imageMetadata(
data: Uint8Array,
src?: string
): Promise<Omit<ImageMetadata, 'src' | 'fsPath'>> {
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,
};
}

0 comments on commit 3a0c02a

Please sign in to comment.