Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add error message for svg without dimensions #10924

Merged
merged 9 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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": minor
---

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,
};
}
3 changes: 1 addition & 2 deletions packages/astro/src/assets/utils/remoteProbe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export async function probe(url: string): Promise<ISize> {
return dimensions;
}
} catch (error) {
// This catch block is specifically for `sizeOf` failures,
// which might occur if the accumulated data isn't yet sufficient.
throw new Error(`Failed to determine the type or the size of the picture (${url}): ${error}`);
Its-Just-Nans marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down