diff options
| author | Chris Lu <chrislusf@users.noreply.github.com> | 2025-10-08 14:24:10 -0700 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-10-08 14:24:10 -0700 |
| commit | 0ce31daf90dc32ec38adf15fe456e6e50f356232 (patch) | |
| tree | 18c41d0c01634c3190e3483a26a20d55bb9b4614 /weed/s3api/s3api_object_handlers_put_test.go | |
| parent | c5f15aaa25badfd577979ea32571887f0969f76c (diff) | |
| download | seaweedfs-0ce31daf90dc32ec38adf15fe456e6e50f356232.tar.xz seaweedfs-0ce31daf90dc32ec38adf15fe456e6e50f356232.zip | |
Fix #7305: Return 400 BadDigest instead of 500 InternalError for MD5 mismatch (#7306)
When an S3 upload has a mismatched Content-MD5 header, SeaweedFS was
incorrectly returning a 500 Internal Server Error instead of the proper
400 Bad Request with error code BadDigest (per AWS S3 specification).
Changes:
- Created weed/util/constants/filer.go with error message constants
- Added ErrMsgBadDigest constant for MD5 mismatch errors
- Added ErrMsgOperationNotPermitted constant for WORM permission errors
- Added ErrBadDigest error code with proper 400 status code mapping
- Updated filerErrorToS3Error() to detect MD5 mismatch and return ErrBadDigest
- Updated filer autoChunk() to return 400 Bad Request for MD5 mismatch
- Refactored error handling to use switch statement for better readability
- Ordered error checks with exact matches first for better maintainability
- Updated all error handling to use centralized constants
- Added comprehensive unit tests
All error messages now use constants from a single location for better
maintainability and consistency. Constants placed in util package to avoid
architectural dependency issues.
Fixes #7305
Diffstat (limited to 'weed/s3api/s3api_object_handlers_put_test.go')
| -rw-r--r-- | weed/s3api/s3api_object_handlers_put_test.go | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/weed/s3api/s3api_object_handlers_put_test.go b/weed/s3api/s3api_object_handlers_put_test.go new file mode 100644 index 000000000..87b874e1f --- /dev/null +++ b/weed/s3api/s3api_object_handlers_put_test.go @@ -0,0 +1,46 @@ +package s3api + +import ( + "testing" + + "github.com/seaweedfs/seaweedfs/weed/s3api/s3err" + "github.com/seaweedfs/seaweedfs/weed/util/constants" +) + +func TestFilerErrorToS3Error(t *testing.T) { + tests := []struct { + name string + errString string + expectedErr s3err.ErrorCode + }{ + { + name: "MD5 mismatch error", + errString: constants.ErrMsgBadDigest, + expectedErr: s3err.ErrBadDigest, + }, + { + name: "Directory exists error", + errString: "existing /path/to/file is a directory", + expectedErr: s3err.ErrExistingObjectIsDirectory, + }, + { + name: "File exists error", + errString: "/path/to/file is a file", + expectedErr: s3err.ErrExistingObjectIsFile, + }, + { + name: "Unknown error", + errString: "some random error", + expectedErr: s3err.ErrInternalError, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := filerErrorToS3Error(tt.errString) + if result != tt.expectedErr { + t.Errorf("filerErrorToS3Error(%q) = %v, want %v", tt.errString, result, tt.expectedErr) + } + }) + } +} |
