Skip to content

armgit / ente

Public Imported from GitHub
0

πŸ’š End-to-end encrypted cloud for everything.

Reduce expected request log noise

Manav Rathi51273ba19336
diff --git a/server/pkg/api/file.go b/server/pkg/api/file.go
index 4c6745fbf9..61edaf5f45 100644
--- a/server/pkg/api/file.go
+++ b/server/pkg/api/file.go
@@ -1,7 +1,6 @@
package api
import (
- "errors"
"fmt"
"net/http"
"os"
@@ -54,9 +53,6 @@ func (h *FileHandler) CreateOrUpdate(c *gin.Context) {
file.IsDeleted = false
file, err := h.Controller.Create(c, userID, file, c.Request.UserAgent(), enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -65,9 +61,6 @@ func (h *FileHandler) CreateOrUpdate(c *gin.Context) {
}
response, err := h.Controller.Update(c, userID, file, enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -93,9 +86,6 @@ func (h *FileHandler) CreateMetaFile(c *gin.Context) {
file.IsDeleted = false
resp, err := h.Controller.CreateMetaFile(c, userID, file, c.Request.UserAgent(), enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -138,9 +128,6 @@ func (h *FileHandler) Update(c *gin.Context) {
}
response, err := h.Controller.Update(c, userID, file, enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -155,9 +142,6 @@ func (h *FileHandler) GetUploadURLs(c *gin.Context) {
count, _ := strconv.Atoi(c.Query("count"))
urls, err := h.Controller.GetUploadURLs(c, userID, count, enteApp, false)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -171,9 +155,6 @@ func (h *FileHandler) ValidateUploadEligibility(c *gin.Context) {
enteApp := auth.GetApp(c)
userID := auth.GetUserID(c.Request.Header)
if err := h.Controller.ValidateUploadEligibility(c, userID, enteApp); err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -191,9 +172,6 @@ func (h *FileHandler) GetUploadURLV2(c *gin.Context) {
}
url, err := h.Controller.GetUploadURLWithMetadata(c, userID, req, enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -208,9 +186,6 @@ func (h *FileHandler) GetMultipartUploadURLs(c *gin.Context) {
count, _ := strconv.Atoi(c.Query("count"))
urls, err := h.Controller.GetMultipartUploadURLs(c, userID, count, enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
@@ -230,24 +205,12 @@ func (h *FileHandler) GetMultipartUploadURLV2(c *gin.Context) {
}
upload, err := h.Controller.GetMultipartUploadURLWithMetadata(c, userID, req, enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
c.JSON(http.StatusOK, upload)
}
-func handleExpectedUploadLimitError(c *gin.Context, err error) bool {
- if errors.Is(err, ente.ErrStorageLimitExceeded) ||
- errors.Is(err, ente.ErrNoActiveSubscription) {
- handler.ExpectedError(c, err)
- return true
- }
- return false
-}
-
// Get redirects the request to the file location
func (h *FileHandler) Get(c *gin.Context) {
userID, fileID := getUserAndFileIDs(c)
@@ -450,9 +413,6 @@ func (h *FileHandler) UpdateThumbnail(c *gin.Context) {
}
err := h.Controller.UpdateThumbnail(c, request.FileID, request.Thumbnail, enteApp)
if err != nil {
- if handleExpectedUploadLimitError(c, err) {
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
diff --git a/server/pkg/api/file_data.go b/server/pkg/api/file_data.go
index 5d1512c736..db7420b773 100644
--- a/server/pkg/api/file_data.go
+++ b/server/pkg/api/file_data.go
@@ -1,7 +1,6 @@
package api
import (
- "errors"
"net/http"
"github.com/ente-io/museum/ente"
@@ -141,10 +140,6 @@ func (h *FileHandler) GetPreviewURL(c *gin.Context) {
actorUser := auth.GetUserID(c.Request.Header)
url, err := h.FileDataCtrl.GetPreviewUrl(c, actorUser, request)
if err != nil {
- if errors.Is(err, &ente.ErrNotFoundError) {
- handler.ExpectedError(c, err)
- return
- }
handler.Error(c, stacktrace.Propagate(err, ""))
return
}
diff --git a/server/pkg/utils/handler/handler.go b/server/pkg/utils/handler/handler.go
index 4a659a8df9..e091a4a1c6 100644
--- a/server/pkg/utils/handler/handler.go
+++ b/server/pkg/utils/handler/handler.go
@@ -19,43 +19,19 @@ import (
// Error parses the error, translates it into an HTTP response and aborts
// the request
func Error(c *gin.Context, err error) {
- errorResponse(c, err, true)
-}
-
-// ExpectedError writes the same HTTP response as Error without logging.
-func ExpectedError(c *gin.Context, err error) {
- errorResponse(c, err, false)
-}
-
-func errorResponse(c *gin.Context, err error, logFailure bool) {
- isClientError := false
- // Tip: To trigger the "unexpected EOF" error, connect with:
- //
- // echo "GET /ping HTTP/1.0\r\nContent-Length: 300\r\n\r\n" | nc localhost 8080
- if errors.Is(err, ente.ErrStorageLimitExceeded) ||
- errors.Is(err, ente.ErrNoActiveSubscription) ||
- errors.Is(err, ente.ErrInvalidPassword) ||
- errors.Is(err, ente.ErrUserDeleted) ||
- errors.Is(err, io.ErrUnexpectedEOF) ||
- errors.Is(err, syscall.EPIPE) ||
- errors.Is(err, syscall.ECONNRESET) {
- isClientError = true
- }
unWrappedErr := errors.Unwrap(err)
if unWrappedErr == nil {
unWrappedErr = err
}
enteApiErr, isEnteApiErr := unWrappedErr.(*ente.ApiError)
- if isEnteApiErr && enteApiErr.HttpStatusCode >= 400 && enteApiErr.HttpStatusCode < 500 {
- isClientError = true
- }
- if logFailure {
+
+ if level, shouldLog := requestFailureLogLevel(err, enteApiErr); shouldLog {
contextLogger := log.WithError(err).
WithFields(log.Fields{
"req_id": requestid.Get(c),
"user_id": auth.GetUserID(c.Request.Header),
})
- if isClientError {
+ if level == log.WarnLevel {
contextLogger.Warn("Request failed")
} else {
contextLogger.Error("Request failed")
@@ -68,7 +44,7 @@ func errorResponse(c *gin.Context, err error, logFailure bool) {
} else {
if _, ok := stacktrace.RootCause(err).(validator.ValidationErrors); ok {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{})
- } else if isClientError {
+ } else if isRequestIOError(err) {
c.AbortWithStatusJSON(http.StatusBadRequest, gin.H{})
} else {
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{})
@@ -76,6 +52,31 @@ func errorResponse(c *gin.Context, err error, logFailure bool) {
}
}
+func requestFailureLogLevel(err error, apiErr *ente.ApiError) (log.Level, bool) {
+ if errors.Is(err, ente.ErrStorageLimitExceeded) ||
+ errors.Is(err, ente.ErrNoActiveSubscription) ||
+ (apiErr != nil && apiErr.Code == ente.NotFoundError) {
+ return log.ErrorLevel, false
+ }
+ if errors.Is(err, ente.ErrInvalidPassword) ||
+ errors.Is(err, ente.ErrUserDeleted) ||
+ errors.Is(err, sql.ErrNoRows) ||
+ isRequestIOError(err) ||
+ (apiErr != nil && apiErr.HttpStatusCode >= 400 && apiErr.HttpStatusCode < 500) {
+ return log.WarnLevel, true
+ }
+ return log.ErrorLevel, true
+}
+
+func isRequestIOError(err error) bool {
+ // Tip: To trigger the "unexpected EOF" error, connect with:
+ //
+ // echo "GET /ping HTTP/1.0\r\nContent-Length: 300\r\n\r\n" | nc localhost 8080
+ return errors.Is(err, io.ErrUnexpectedEOF) ||
+ errors.Is(err, syscall.EPIPE) ||
+ errors.Is(err, syscall.ECONNRESET)
+}
+
// If `err` directly maps to an HTTP status code, return the HTTP status code.
// Otherwise return 0.
func httpStatusCode(err error) int {
diff --git a/server/pkg/utils/handler/handler_test.go b/server/pkg/utils/handler/handler_test.go
index 4ec8289bad..b89ad3be77 100644
--- a/server/pkg/utils/handler/handler_test.go
+++ b/server/pkg/utils/handler/handler_test.go
@@ -1,7 +1,9 @@
package handler
import (
+ "database/sql"
"encoding/json"
+ "errors"
"net/http"
"net/http/httptest"
"testing"
@@ -14,32 +16,80 @@ import (
"github.com/stretchr/testify/require"
)
-func TestErrorLogsWarnForNotFoundAPIError(t *testing.T) {
+func TestErrorDoesNotLogExpectedErrors(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ for _, tt := range []struct {
+ name string
+ err error
+ wantStatus int
+ }{
+ {
+ name: "storage limit",
+ err: ente.ErrStorageLimitExceeded,
+ wantStatus: http.StatusUpgradeRequired,
+ },
+ {
+ name: "no active subscription",
+ err: ente.ErrNoActiveSubscription,
+ wantStatus: http.StatusPaymentRequired,
+ },
+ {
+ name: "not found api error",
+ err: ente.ErrNotFoundError.NewErr("missing"),
+ wantStatus: http.StatusNotFound,
+ },
+ } {
+ t.Run(tt.name, func(t *testing.T) {
+ recorder, ctx := testContext()
+ hook := testLogHook(t)
+
+ Error(ctx, stacktrace.Propagate(tt.err, ""))
+
+ require.Equal(t, tt.wantStatus, recorder.Code)
+ require.Empty(t, hook.AllEntries())
+ })
+ }
+}
+
+func TestErrorLogsWarnForSQLNoRows(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder, ctx := testContext()
hook := testLogHook(t)
- Error(ctx, stacktrace.Propagate(&ente.ErrNotFoundError, ""))
+ Error(ctx, stacktrace.Propagate(sql.ErrNoRows, ""))
require.Equal(t, http.StatusNotFound, recorder.Code)
-
entry := hook.LastEntry()
require.NotNil(t, entry)
require.Equal(t, log.WarnLevel, entry.Level)
require.Equal(t, "Request failed", entry.Message)
}
-func TestExpectedErrorPreservesNotFoundAPIErrorResponseWithoutLog(t *testing.T) {
+func TestErrorLogsUnexpectedErrors(t *testing.T) {
gin.SetMode(gin.TestMode)
recorder, ctx := testContext()
hook := testLogHook(t)
- ExpectedError(ctx, stacktrace.Propagate(&ente.ErrNotFoundError, ""))
+ Error(ctx, errors.New("boom"))
+
+ require.Equal(t, http.StatusInternalServerError, recorder.Code)
+ entry := hook.LastEntry()
+ require.NotNil(t, entry)
+ require.Equal(t, log.ErrorLevel, entry.Level)
+ require.Equal(t, "Request failed", entry.Message)
+}
+
+func TestErrorPreservesNotFoundAPIErrorResponse(t *testing.T) {
+ gin.SetMode(gin.TestMode)
+
+ recorder, ctx := testContext()
+
+ Error(ctx, stacktrace.Propagate(&ente.ErrNotFoundError, ""))
require.Equal(t, http.StatusNotFound, recorder.Code)
- require.Empty(t, hook.AllEntries())
var body ente.ApiError
require.NoError(t, json.Unmarshal(recorder.Body.Bytes(), &body))