0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-01-20 22:12:38 -05:00

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>
This commit is contained in:
n4n5 2024-05-16 12:52:35 +02:00 committed by GitHub
parent 303968bd72
commit 3a0c02ae03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 12 deletions

View file

@ -0,0 +1,5 @@
---
"astro": patch
---
Handle image-size errors by displaying a clearer message

View file

@ -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,
};
}