From a6f9a0261aae4f94ccaf634756f639f4b5ff6251 Mon Sep 17 00:00:00 2001 From: Nick Mills-Barrett Date: Mon, 21 Feb 2022 15:04:36 +0000 Subject: [PATCH] Return a 503 when media uploads fail. --- api/r0/upload.go | 2 +- api/responses.go | 4 ++++ api/webserver/route_handler.go | 3 +++ common/errorcodes.go | 1 + 4 files changed, 9 insertions(+), 1 deletion(-) diff --git a/api/r0/upload.go b/api/r0/upload.go index 63b0170b..9fa659ec 100644 --- a/api/r0/upload.go +++ b/api/r0/upload.go @@ -69,7 +69,7 @@ func UploadMedia(r *http.Request, rctx rcontext.RequestContext, user api.UserInf rctx.Log.Error("Unexpected error storing media: " + err.Error()) sentry.CaptureException(err) - return api.InternalServerError("Unexpected Error") + return api.ServiceUnavailable() } if rctx.Config.Features.MSC2448Blurhash.Enabled && r.URL.Query().Get("xyz.amorgan.generate_blurhash") == "true" { diff --git a/api/responses.go b/api/responses.go index 93d2c39c..8a8feea0 100644 --- a/api/responses.go +++ b/api/responses.go @@ -57,3 +57,7 @@ func BadRequest(message string) *ErrorResponse { func QuotaExceeded() *ErrorResponse { return &ErrorResponse{common.ErrCodeForbidden, "Quota Exceeded", common.ErrCodeQuotaExceeded} } + +func ServiceUnavailable() *ErrorResponse { + return &ErrorResponse{common.ErrCodeServiceUnavailable, "Service unavailable", common.ErrCodeServiceUnavailable} +} diff --git a/api/webserver/route_handler.go b/api/webserver/route_handler.go index 3d04714b..3afed9ee 100644 --- a/api/webserver/route_handler.go +++ b/api/webserver/route_handler.go @@ -161,6 +161,9 @@ func (h handler) ServeHTTP(w http.ResponseWriter, r *http.Request) { case common.ErrCodeForbidden: statusCode = http.StatusForbidden break + case common.ErrCodeServiceUnavailable: + statusCode = http.StatusServiceUnavailable + break default: // Treat as unknown (a generic server error) statusCode = http.StatusInternalServerError break diff --git a/common/errorcodes.go b/common/errorcodes.go index 599b32c9..70c75882 100644 --- a/common/errorcodes.go +++ b/common/errorcodes.go @@ -16,3 +16,4 @@ const ErrCodeRateLimitExceeded = "M_LIMIT_EXCEEDED" const ErrCodeUnknown = "M_UNKNOWN" const ErrCodeForbidden = "M_FORBIDDEN" const ErrCodeQuotaExceeded = "M_QUOTA_EXCEEDED" +const ErrCodeServiceUnavailable = "M_SERVICE_UNAVAILABLE"