Files
seaweedfs/weed/s3api/s3api_object_handlers_put_test.go
Chris Lu 8bf727d225 Fix #7060: Return 400 InvalidRequest instead of 500 for context canceled errors (#7309)
When a client cancels an HTTP request (e.g., connection timeout, client
disconnect), the context gets canceled and propagates through the system
as "context canceled" or "code = Canceled" errors. These errors were
being treated as internal server errors (500) when they should be treated
as client errors (400).

Problem:
- Client cancels request or connection times out
- Filer fails to assign file ID with "context canceled"
- S3 API returns HTTP 500 Internal Server Error
- This is incorrect - it's a client issue, not a server issue

Solution:
Added detection for context canceled errors in filerErrorToS3Error():
- Detects "context canceled" and "code = Canceled" in error strings
- Returns ErrInvalidRequest (HTTP 400) instead of ErrInternalError (500)
- Properly attributes the error to the client, not the server

Changes:
- Updated filerErrorToS3Error() to detect context cancellation
- Added test cases for both gRPC and simple context canceled errors
- Maintains existing error handling for other error types

This ensures:
- Clients get appropriate 4xx error codes for their canceled requests
- Server metrics correctly reflect that these are client issues
- Monitoring/alerting won't trigger false positives for client timeouts

Fixes #7060
2025-10-08 21:18:41 -07:00

57 lines
1.4 KiB
Go

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: "Context canceled error",
errString: "rpc error: code = Canceled desc = context canceled",
expectedErr: s3err.ErrInvalidRequest,
},
{
name: "Context canceled error (simple)",
errString: "context canceled",
expectedErr: s3err.ErrInvalidRequest,
},
{
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)
}
})
}
}