From 0b8c8e0cc2c9619d4446830a00794bce69d7aadb Mon Sep 17 00:00:00 2001 From: Michael Bumann Date: Wed, 17 Apr 2024 15:53:18 +0200 Subject: [PATCH] Use proper JSON response for rate limit error --- lib/responses/errors.go | 7 +++++++ lib/transport/echo.go | 4 ++++ 2 files changed, 11 insertions(+) diff --git a/lib/responses/errors.go b/lib/responses/errors.go index 3522edb7..222cde1c 100644 --- a/lib/responses/errors.go +++ b/lib/responses/errors.go @@ -99,6 +99,13 @@ var AccountDeactivatedError = ErrorResponse{ HttpStatusCode: 401, } +var TooManyRequestsError = ErrorResponse{ + Error: true, + Code: 1, + Message: "Too many requests", + HttpStatusCode: 429, +} + func HTTPErrorHandler(err error, c echo.Context) { if c.Response().Committed { return diff --git a/lib/transport/echo.go b/lib/transport/echo.go index 53e94040..6b56d093 100644 --- a/lib/transport/echo.go +++ b/lib/transport/echo.go @@ -77,6 +77,10 @@ func CreateRateLimitMiddleware(requestsPerSecond int, burst int) echo.Middleware return id, nil }, + ErrorHandler: func(context echo.Context, err error) error { + res := responses.TooManyRequestsError + return context.JSON(res.HttpStatusCode, res) + }, } return middleware.RateLimiterWithConfig(config)