0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2025-03-31 23:31:30 -05:00

Descriptive image error (#9352)

Co-authored-by: Florian Lefebvre <contact@florian-lefebvre.dev>
Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
Co-authored-by: Princesseuh <3019731+Princesseuh@users.noreply.github.com>
This commit is contained in:
Tom MacWright 2023-12-19 11:14:58 -05:00 committed by GitHub
parent 354a62c86e
commit f515b1421a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 63 additions and 21 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Add a more descriptive error message when image conversion fails

View file

@ -153,7 +153,7 @@
"mime": "^3.0.0",
"ora": "^7.0.1",
"p-limit": "^5.0.0",
"p-queue": "^7.4.1",
"p-queue": "^8.0.1",
"path-to-regexp": "^6.2.1",
"preferred-pm": "^3.1.2",
"probe-image-size": "^7.2.3",

View file

@ -6,6 +6,8 @@ import type { AstroConfig } from '../../@types/astro.js';
import type { BuildPipeline } from '../../core/build/buildPipeline.js';
import { getOutDirWithinCwd } from '../../core/build/common.js';
import { getTimeStat } from '../../core/build/util.js';
import { AstroError } from '../../core/errors/errors.js';
import { AstroErrorData } from '../../core/errors/index.js';
import type { Logger } from '../../core/logger/core.js';
import { isRemotePath, prependForwardSlash } from '../../core/path.js';
import { isServerLikeOutput } from '../../prerender/utils.js';
@ -103,9 +105,11 @@ export async function generateImagesForPath(
const originalImageData = await loadImage(originalFilePath, env);
for (const [_, transform] of transformsAndPath.transforms) {
queue.add(async () =>
generateImage(originalImageData, transform.finalPath, transform.transform)
);
await queue
.add(async () => generateImage(originalImageData, transform.finalPath, transform.transform))
.catch((e) => {
throw e;
});
}
// In SSR, we cannot know if an image is referenced in a server-rendered page, so we can't delete anything
@ -207,13 +211,27 @@ export async function generateImagesForPath(
};
const imageService = (await getConfiguredImageService()) as LocalImageService;
resultData.data = (
await imageService.transform(
originalImage.data,
{ ...options, src: originalImagePath },
env.imageConfig
)
).data;
try {
resultData.data = (
await imageService.transform(
originalImage.data,
{ ...options, src: originalImagePath },
env.imageConfig
)
).data;
} catch (e) {
const error = new AstroError(
{
...AstroErrorData.CouldNotTransformImage,
message: AstroErrorData.CouldNotTransformImage.message(originalFilePath),
},
undefined,
{ cause: e }
);
throw error;
}
try {
// Write the cache entry

View file

@ -71,6 +71,7 @@ export const GET: APIRoute = async ({ request }) => {
},
});
} catch (err: unknown) {
console.error('Could not process image request:', err);
return new Response(`Server Error: ${err}`, { status: 500 });
}
};

View file

@ -89,6 +89,7 @@ export const GET: APIRoute = async ({ request }) => {
},
});
} catch (err: unknown) {
console.error('Could not process image request:', err);
return new Response(`Server Error: ${err}`, { status: 500 });
}
};

View file

@ -17,7 +17,7 @@ export async function getConfiguredImageService(): Promise<ImageService> {
'virtual:image-service'
).catch((e) => {
const error = new AstroError(AstroErrorData.InvalidImageService);
(error as any).cause = e;
error.cause = e;
throw error;
});

View file

@ -706,6 +706,23 @@ export const MarkdownImageNotFound = {
hint: 'This is often caused by a typo in the image path. Please make sure the file exists, and is spelled correctly.',
} satisfies ErrorData;
/**
* @docs
* @see
* - [Images](https://docs.astro.build/en/guides/images/)
* @description
* Astro could not transform one of your images. Often, this is caused by a corrupted or malformed image. Re-exporting the image from your image editor may fix this issue.
*
* Depending on the image service you are using, the stack trace may contain more information on the specific error encountered.
*/
export const CouldNotTransformImage = {
name: 'CouldNotTransformImage',
title: 'Could not transform image.',
message: (imagePath: string) =>
`Could not transform image \`${imagePath}\`. See the stack trace for more information.`,
hint: 'This is often caused by a corrupted or malformed image. Re-exporting the image from your image editor may fix this issue.',
} satisfies ErrorData;
/**
* @docs
* @description

18
pnpm-lock.yaml generated
View file

@ -596,8 +596,8 @@ importers:
specifier: ^5.0.0
version: 5.0.0
p-queue:
specifier: ^7.4.1
version: 7.4.1
specifier: ^8.0.1
version: 8.0.1
path-to-regexp:
specifier: ^6.2.1
version: 6.2.1
@ -12964,17 +12964,17 @@ packages:
aggregate-error: 4.0.1
dev: true
/p-queue@7.4.1:
resolution: {integrity: sha512-vRpMXmIkYF2/1hLBKisKeVYJZ8S2tZ0zEAmIJgdVKP2nq0nh4qCdf8bgw+ZgKrkh71AOCaqzwbJJk1WtdcF3VA==}
engines: {node: '>=12'}
/p-queue@8.0.1:
resolution: {integrity: sha512-NXzu9aQJTAzbBqOt2hwsR63ea7yvxJc0PwN/zobNAudYfb1B7R08SzB4TsLeSbUCuG467NhnoT0oO6w1qRO+BA==}
engines: {node: '>=18'}
dependencies:
eventemitter3: 5.0.1
p-timeout: 5.1.0
p-timeout: 6.1.2
dev: false
/p-timeout@5.1.0:
resolution: {integrity: sha512-auFDyzzzGZZZdHz3BtET9VEz0SE/uMEAx7uWfGPucfzEwwe/xH0iVeZibQmANYE/hp9T2+UUZT5m+BKyrDp3Ew==}
engines: {node: '>=12'}
/p-timeout@6.1.2:
resolution: {integrity: sha512-UbD77BuZ9Bc9aABo74gfXhNvzC9Tx7SxtHSh1fxvx3jTLLYvmVhiQZZrJzqqU0jKbN32kb5VOKiLEQI/3bIjgQ==}
engines: {node: '>=14.16'}
dev: false
/p-try@2.2.0: