Skip to content

feat(image-adapter): improve error handling and status codes #893

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

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

iDVB
Copy link

@iDVB iDVB commented Jun 6, 2025

Moved to new PR against a non-main branch.
Original PR here: #886

Fixes #885

Copy link

changeset-bot bot commented Jun 6, 2025

🦋 Changeset detected

Latest commit: aa36783

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 3 packages
Name Type
@opennextjs/aws Major
app-pages-router Patch
app-router Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

pkg-pr-new bot commented Jun 6, 2025

Open in StackBlitz

pnpm add https://pkg.pr.new/@opennextjs/aws@893

commit: aa36783

@vicb vicb changed the title Fix 885 feat(image-adapter): improve error handling and status codes Jun 6, 2025
Copy link
Contributor

@vicb vicb left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you please add a changeset? (pnpm changeset)

Thanks!

@iDVB
Copy link
Author

iDVB commented Jun 12, 2025

Just an update. This seems to be working the dream in our prod environment so far.

Copy link
Contributor

@conico974 conico974 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry to be this nitpicky on this one, but that should be the last round of change

try {
// Case 1: remote image URL => download the image from the URL
// EXTERNAL IMAGE HANDLING
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// EXTERNAL IMAGE HANDLING
// remote image URL => download the image from the URL


// INTERNAL IMAGE HANDLING (S3)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// INTERNAL IMAGE HANDLING (S3)
// local image => download the image from the provided ImageLoader (default is S3)

}
const message = "Empty response body from the S3 request.";
const clientError = new IgnorableError(message, 400);
error("Empty response from S3", clientError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error("Empty response from S3", clientError);
error("Empty response from ImageLoader", clientError);


// Special handling for S3 ListBucket permission errors
// AWS SDK v3 nests error details deeply within the error object
const isListBucketError =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the S3 related stuff should not be here. https://github.com/opennextjs/opennextjs-aws/blob/main/packages/open-next/src/overrides/imageLoader/s3.ts
This is the ImageLoader and where you should handle S3 related error, not here.
Here we should only handle our error, or basic error


// Still include error details in headers for debugging only
const errorMessage = isListBucketError ? "Access denied" : message;
res.setHeader("x-nextjs-internal-error", errorMessage);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this one propagates to the end user ?

@@ -114,10 +115,19 @@ export async function defaultHandler(
);
return buildSuccessResponse(result, options?.streamCreator, etag);
} catch (e: any) {
error("Failed to optimize image", e);
// All image-related errors should be treated as client errors
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not true, if something fails on the server it is not client errors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Image Optimization Adapter Returns Improper HTTP Status Codes
4 participants