Skip to content

Commit

Permalink
Improve VerifyQuota errors (#68)
Browse files Browse the repository at this point in the history
* Improve VerifyQuota errors

* Use lowercase instead of _Prefix for const
  • Loading branch information
VojtechVitek authored Nov 12, 2024
1 parent 00d47a3 commit c2dc659
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 21 deletions.
16 changes: 2 additions & 14 deletions middleware/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ package middleware

import (
"context"
"encoding/json"
"net/http"
"time"

"github.com/0xsequence/quotacontrol/proto"
"github.com/goware/logger"

"github.com/0xsequence/authcontrol"
)

const (
Expand All @@ -19,7 +16,7 @@ const (
type Options struct {
Logger logger.Logger
BaseRequestCost int
ErrHandler authcontrol.ErrHandler
ErrHandler func(r *http.Request, w http.ResponseWriter, err error)
}

func (o *Options) ApplyDefaults() {
Expand All @@ -33,16 +30,7 @@ func (o *Options) ApplyDefaults() {
}

func errHandler(r *http.Request, w http.ResponseWriter, err error) {
rpcErr, ok := err.(proto.WebRPCError)
if !ok {
rpcErr = proto.ErrWebrpcEndpoint.WithCause(err)
}

w.Header().Set("Content-Type", "application/json")
w.WriteHeader(rpcErr.HTTPStatus)

respBody, _ := json.Marshal(rpcErr)
w.Write(respBody)
proto.RespondWithError(w, err)
}

// Client is the interface that wraps the basic FetchKeyQuota, GetUsage and SpendQuota methods.
Expand Down
9 changes: 4 additions & 5 deletions middleware/middleware_quota.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import (
"net/http"
"strconv"

"github.com/0xsequence/quotacontrol/proto"

"github.com/0xsequence/authcontrol"
"github.com/0xsequence/quotacontrol/proto"
)

func VerifyQuota(client Client, o Options) func(next http.Handler) http.Handler {
Expand All @@ -27,7 +26,7 @@ func VerifyQuota(client Client, o Options) func(next http.Handler) http.Handler
if session == proto.SessionType_Project {
id, ok := authcontrol.GetProjectID(ctx)
if !ok {
o.ErrHandler(r, w, proto.ErrUnauthorizedUser)
o.ErrHandler(r, w, proto.ErrUnauthorizedUser.WithCausef("verify quota: no project ID found in context"))
return
}
projectID = id
Expand All @@ -41,7 +40,7 @@ func VerifyQuota(client Client, o Options) func(next http.Handler) http.Handler
if q != nil {
if ok, err := client.CheckPermission(ctx, projectID, proto.UserPermission_READ); !ok {
if err == nil {
err = proto.ErrUnauthorizedUser
err = proto.ErrUnauthorizedUser.WithCausef("verify quota: no read permission")
}
o.ErrHandler(r, w, err)
return
Expand All @@ -53,7 +52,7 @@ func VerifyQuota(client Client, o Options) func(next http.Handler) http.Handler
if session.Is(proto.SessionType_AccessKey, proto.SessionType_Project) {
accessKey, ok := authcontrol.GetAccessKey(ctx)
if !ok && session == proto.SessionType_AccessKey {
o.ErrHandler(r, w, proto.ErrUnauthorizedUser)
o.ErrHandler(r, w, proto.ErrUnauthorizedUser.WithCausef("verify quota: no access key found in context"))
return
}

Expand Down
4 changes: 2 additions & 2 deletions middleware/middleware_ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
HeaderRetryAfter = "Retry-After"
)

const _RateLimitWindow = 1 * time.Minute
const rateLimitWindow = 1 * time.Minute

const (
DefaultPublicRate = 3000
Expand Down Expand Up @@ -101,7 +101,7 @@ func RateLimit(cfg RateLimitConfig, counter httprate.LimitCounter, o Options) fu
}),
}

limiter := httprate.NewRateLimiter(cfg.PublicRPM, _RateLimitWindow, options...)
limiter := httprate.NewRateLimiter(cfg.PublicRPM, rateLimitWindow, options...)

// The rate limiter middleware
return func(next http.Handler) http.Handler {
Expand Down

0 comments on commit c2dc659

Please sign in to comment.