-
-
Notifications
You must be signed in to change notification settings - Fork 2k
chore: Improve error messages in SaveFileToStorage #4173
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
base: main
Are you sure you want to change the base?
Changes from 4 commits
80e0203
485129f
23f1d53
240b1ad
d310941
3364650
3d08591
3cf3871
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||
| defer file.Close() //nolint:errcheck // not needed | ||
|
|
||
|
|
@@ -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
|
||
|
|
||
| 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
|
||
|
|
||
| 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) | ||
|
pratikramteke marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| return nil | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.