Skip to content

chore: Improve error messages in SaveFileToStorage#4173

Open
pratikramteke wants to merge 7 commits intogofiber:mainfrom
pratikramteke:improve-error-messages
Open

chore: Improve error messages in SaveFileToStorage#4173
pratikramteke wants to merge 7 commits intogofiber:mainfrom
pratikramteke:improve-error-messages

Conversation

@pratikramteke
Copy link
Copy Markdown

Improve error messages in SaveFileToStorage by including filename and path.

This provides better debugging context when handling file upload errors.

@pratikramteke pratikramteke requested a review from a team as a code owner April 1, 2026 08:48
@ReneWerner87 ReneWerner87 added this to v3 Apr 1, 2026
@ReneWerner87 ReneWerner87 added this to the v3 milestone Apr 1, 2026
Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request enhances error reporting in the SaveFileToStorage function by including the filename and storage path in error messages. The reviewer recommends using the %q format verb instead of %s for these strings to ensure proper quoting and escaping of user-provided data, and highlights a potential information disclosure risk when including internal paths in errors.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Apr 1, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 60bff7f3-c461-41d4-83be-2dae1c349f83

📥 Commits

Reviewing files that changed from the base of the PR and between d310941 and 3d08591.

📒 Files selected for processing (3)
  • ctx.go
  • ctx_test.go
  • error.go
✅ Files skipped from review due to trivial changes (1)
  • ctx_test.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • ctx.go

Walkthrough

Added explicit nil check in DefaultCtx.SaveFileToStorage and introduced four new exported sentinel file errors; SaveFileToStorage now wraps and formats errors to include multipart filename (and destination path for store errors). Added tests for nil fileheader and BodyLimit filename propagation. No public signatures changed.

Changes

Cohort / File(s) Summary
SaveFileToStorage implementation
ctx.go
Added early return with ErrFileHeaderNil when fileheader is nil; introduced and used ErrFileOpen, ErrFileRead, ErrFileStore for wrapped errors; error messages now include fileheader.Filename and, for storage failures, the destination path.
File error declarations
error.go
Added four exported error variables: ErrFileHeaderNil, ErrFileOpen, ErrFileRead, ErrFileStore.
Tests for SaveFileToStorage
ctx_test.go
Added tests asserting nil fileheader returns an error mentioning "nil" and that BodyLimit/read error messages include the original multipart filename ("test-file.png").

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related PRs

Suggested reviewers

  • gaby
  • sixcolors
  • ReneWerner87

Poem

🐰 I nibble bytes along the stream,

filenames sparkle in my dream,
I name the path and sing each flaw,
hop, report — precise and raw,
tiny paws applaud the clean-up theme.

🚥 Pre-merge checks | ✅ 1 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The PR description is minimal and lacks most required template sections including 'Changes introduced', 'Type of change', and 'Checklist' items that should be completed. Complete the description template by adding 'Changes introduced' section, selecting appropriate 'Type of change' (likely 'Enhancement' and 'Code consistency'), and checking off relevant 'Checklist' items to confirm testing and documentation updates.
Docstring Coverage ⚠️ Warning Docstring coverage is 33.33% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (1 passed)
Check name Status Explanation
Title check ✅ Passed The title 'chore: Improve error messages in SaveFileToStorage' directly and clearly describes the main change in the PR - improvements to error messages in the SaveFileToStorage function.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

pratikramteke and others added 3 commits April 1, 2026 14:26
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
ctx.go Outdated
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)
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

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 1, 2026

Codecov Report

❌ Patch coverage is 57.14286% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 91.22%. Comparing base (bf952a1) to head (3364650).

Files with missing lines Patch % Lines
ctx.go 57.14% 3 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main    #4173   +/-   ##
=======================================
  Coverage   91.22%   91.22%           
=======================================
  Files         123      123           
  Lines       11832    11834    +2     
=======================================
+ Hits        10794    10796    +2     
  Misses        653      653           
  Partials      385      385           
Flag Coverage Δ
unittests 91.22% <57.14%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ReneWerner87
Copy link
Copy Markdown
Member

@pratikramteke pls check the hints

@gaby gaby changed the title improve error messages in SaveFileToStorage chore: Improve error messages in SaveFileToStorage Apr 2, 2026
@gaby
Copy link
Copy Markdown
Member

gaby commented Apr 2, 2026

@pratikramteke The error needs to be defined in error.go. Then returned that predefined error.

@efectn
Copy link
Copy Markdown
Member

efectn commented Apr 3, 2026

Please check unit tests and lintci errors

file, err := fileheader.Open()
if err != nil {
return fmt.Errorf("failed to open: %w", err)
return fmt.Errorf("%w: %q: %v", ErrFileOpen, fileheader.Filename, err)
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.

i think we better sanitize filename for security reasons

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.

The filename comes from the Client Request, so they already know it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

4 participants