Skip to content
10 changes: 5 additions & 5 deletions ctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ func (*DefaultCtx) SaveFile(fileheader *multipart.FileHeader, path string) error
func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path string, storage Storage) error {
file, err := fileheader.Open()
if err != nil {
return fmt.Errorf("failed to open: %w", err)
return fmt.Errorf("failed to open file %s: %w", fileheader.Filename, err)
Comment thread
pratikramteke marked this conversation as resolved.
Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

If the fileader is nil, it is going to panic. you also need to add check for that. In addition to panic check, could you also unit test cases for error messages?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't err have the filename already?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

no it only shows what the error is https://pkg.go.dev/internal/oserror#pkg-variables

}
Comment on lines 524 to 527
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

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

The underlying err from fileheader.Open() is formatted with %v, so it is not wrapped. This breaks errors.Is/As for callers and makes it harder to detect the root cause programmatically. Wrap the underlying error (use %w) while still including the filename in the message (Go supports multiple %w).

Copilot uses AI. Check for mistakes.
defer file.Close() //nolint:errcheck // not needed

Expand All @@ -529,25 +529,25 @@ func (c *DefaultCtx) SaveFileToStorage(fileheader *multipart.FileHeader, path st
}

if fileheader.Size > 0 && fileheader.Size > int64(maxUploadSize) {
return fmt.Errorf("failed to read: %w", fasthttp.ErrBodyTooLarge)
return fmt.Errorf("failed to read file %q: %w", fileheader.Filename, fasthttp.ErrBodyTooLarge)
}
Comment on lines 524 to 537
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

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

PR description says errors should include both filename and path, but the open/read error paths only include fileheader.Filename (the path argument isn’t mentioned until the store failure case). Either include path in these earlier error messages too, or adjust the PR description/tests to match the intended behavior.

Copilot uses AI. Check for mistakes.
Comment on lines 535 to 537
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

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

fasthttp.ErrBodyTooLarge is included using %v, so the returned error no longer wraps it. Existing callers/tests that rely on errors.Is(err, fasthttp.ErrBodyTooLarge) will fail. Use %w for fasthttp.ErrBodyTooLarge (and keep wrapping ErrFileRead).

Copilot uses AI. Check for mistakes.

buf := bytebufferpool.Get()
defer bytebufferpool.Put(buf)

limitedReader := io.LimitReader(file, int64(maxUploadSize)+1)
if _, err = buf.ReadFrom(limitedReader); err != nil {
return fmt.Errorf("failed to read: %w", err)
return fmt.Errorf("failed to read file %q: %w", fileheader.Filename, err)
}

if buf.Len() > maxUploadSize {
return fmt.Errorf("failed to read: %w", fasthttp.ErrBodyTooLarge)
return fmt.Errorf("failed to read file %q: %w", fileheader.Filename, fasthttp.ErrBodyTooLarge)
}
Comment on lines 547 to 549
Copy link

Copilot AI Apr 26, 2026

Choose a reason for hiding this comment

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

When buf.Len() > maxUploadSize, fasthttp.ErrBodyTooLarge is again included with %v instead of being wrapped. This prevents errors.Is(err, fasthttp.ErrBodyTooLarge) from working. Wrap it with %w (and keep wrapping ErrFileRead).

Copilot uses AI. Check for mistakes.

data := append([]byte(nil), buf.Bytes()...)

if err := storage.SetWithContext(c.Context(), path, data, 0); err != nil {
return fmt.Errorf("failed to store: %w", err)
return fmt.Errorf("failed to store file %s at %s: %w", fileheader.Filename, path, err)
Comment thread
pratikramteke marked this conversation as resolved.
Outdated
}

return nil
Expand Down
Loading