Skip to content

Commit

Permalink
Descriptive image error (#9352)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
4 people committed Dec 19, 2023
1 parent 354a62c commit f515b14
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 21 deletions.
5 changes: 5 additions & 0 deletions .changeset/famous-cars-sell.md
@@ -0,0 +1,5 @@
---
'astro': patch
---

Add a more descriptive error message when image conversion fails
2 changes: 1 addition & 1 deletion packages/astro/package.json
Expand Up @@ -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",
Expand Down
38 changes: 28 additions & 10 deletions packages/astro/src/assets/build/generate.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/assets/endpoint/generic.ts
Expand Up @@ -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 });
}
};
1 change: 1 addition & 0 deletions packages/astro/src/assets/endpoint/node.ts
Expand Up @@ -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 });
}
};
2 changes: 1 addition & 1 deletion packages/astro/src/assets/internal.ts
Expand Up @@ -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;
});

Expand Down
17 changes: 17 additions & 0 deletions packages/astro/src/core/errors/errors-data.ts
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f515b14

Please sign in to comment.