π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Classify canceled requests as client closed
Neeraj Gupta1c1250c8d2e4
diff --git a/server/pkg/utils/handler/handler.go b/server/pkg/utils/handler/handler.goindex 83782f5eda..d8d5fd1861 100644--- a/server/pkg/utils/handler/handler.go+++ b/server/pkg/utils/handler/handler.go@@ -1,6 +1,7 @@package handlerimport (+ "context""database/sql""errors""io"@@ -18,6 +19,8 @@ import (var errBindJSON = errors.New("bind json")+const statusClientClosedRequest = 499+type bindJSONError struct {err error}@@ -42,6 +45,11 @@ func BindJSON(c *gin.Context, obj any) error {}func Error(c *gin.Context, err error) {+ if c.Request.Context().Err() == context.Canceled {+ c.AbortWithStatus(statusClientClosedRequest)+ return+ }+unWrappedErr := errors.Unwrap(err)if unWrappedErr == nil {unWrappedErr = errdiff --git a/server/pkg/utils/handler/handler_test.go b/server/pkg/utils/handler/handler_test.goindex 5fb7e419f7..3834e520ef 100644--- a/server/pkg/utils/handler/handler_test.go+++ b/server/pkg/utils/handler/handler_test.go@@ -1,6 +1,7 @@package handlerimport (+ "context""database/sql""encoding/json""errors"@@ -168,6 +169,21 @@ func TestErrorLogsUnexpectedErrors(t *testing.T) {require.Equal(t, "Request failed", entry.Message)}+func TestErrorDoesNotReportCanceledRequestAsServerFailure(t *testing.T) {+ gin.SetMode(gin.TestMode)++ recorder, ctx := testContext()+ requestContext, cancel := context.WithCancel(ctx.Request.Context())+ ctx.Request = ctx.Request.WithContext(requestContext)+ cancel()+ hook := testLogHook(t)++ Error(ctx, errors.New("database query canceled"))++ require.Equal(t, statusClientClosedRequest, recorder.Code)+ require.Empty(t, hook.AllEntries())+}+func TestErrorPreservesNotFoundAPIErrorResponse(t *testing.T) {gin.SetMode(gin.TestMode)