Skip to content

Commit

Permalink
Merge pull request #488 from getAlby/rate-limit-response
Browse files Browse the repository at this point in the history
Use proper JSON response for rate limit error
  • Loading branch information
bumi authored Jun 7, 2024
2 parents badb700 + 0b8c8e0 commit 8fe603c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/responses/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/transport/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8fe603c

Please sign in to comment.