diff --git a/const.go b/const.go index 8d32fa2..5128c56 100644 --- a/const.go +++ b/const.go @@ -18,3 +18,14 @@ const ( OperationPatch = "PATCH" OperationDelete = "DELETE" ) + +const ( + unknownLimiterRoleError = "unknown limiter role operation" + unknownLimiterError = "unknown limiter operation" + unsupportedOperationError = "unsupported operation" + requiredNameDestinationError = "name and destination is required" + requiredLimiterIDError = "limiter id is required" + createProxyError = "Error creating proxy request" + sendingProxyError = "Error sending proxy request" + slowDownError = "slow down" +) diff --git a/golim.go b/golim.go index 9e65d56..a328a2d 100644 --- a/golim.go +++ b/golim.go @@ -89,7 +89,7 @@ func (g *golim) ExecCMD(ctx context.Context) (interface{}, error) { if g.limiterRole != nil { return handleLimiterRoleOperation(g, ctx) } - return nil, errors.New("unsupported operation") + return nil, errors.New(unsupportedOperationError) } func handleLimiterOperation(g *golim, ctx context.Context) (interface{}, error) { @@ -99,7 +99,7 @@ func handleLimiterOperation(g *golim, ctx context.Context) (interface{}, error) case removeLimiterOperation: return nil, g.removeRateLimiter(ctx) } - return nil, errors.New("unknown limiter operation") + return nil, errors.New(unknownLimiterError) } func handleLimiterRoleOperation(g *golim, ctx context.Context) (interface{}, error) { @@ -111,7 +111,7 @@ func handleLimiterRoleOperation(g *golim, ctx context.Context) (interface{}, err case getRolesOperationID: return g.getRoles(ctx) } - return nil, errors.New("unknown limiter role operation") + return nil, errors.New(unknownLimiterRoleError) } func newLimiter(db *sql.DB, cache *cache) *golim { @@ -174,7 +174,7 @@ func (g *golim) createInitCMD() *ff.Command { operation: createLimiterOperation, } } else { - return errors.New("name and destination is required") + return errors.New(requiredNameDestinationError) } g.skip = true return nil @@ -283,7 +283,7 @@ func (g *golim) createGetRolesCMD() *ff.Command { limiterID: *limiterID, } } else { - return errors.New("limiter id is required") + return errors.New(requiredLimiterIDError) } g.skip = true return nil diff --git a/proxy.go b/proxy.go index c5b99c1..8a7ce68 100644 --- a/proxy.go +++ b/proxy.go @@ -21,7 +21,7 @@ func runProxy(g *golim) func(w http.ResponseWriter, r *http.Request) { endPoint: path, } if !isOkRequest(r, g) { - http.Error(w, "slow down", http.StatusTooManyRequests) + http.Error(w, slowDownError, http.StatusTooManyRequests) return } @@ -39,7 +39,7 @@ func runProxy(g *golim) func(w http.ResponseWriter, r *http.Request) { targetURL := newURL proxyReq, err := http.NewRequest(r.Method, targetURL.String(), r.Body) if err != nil { - http.Error(w, "Error creating proxy request", http.StatusInternalServerError) + http.Error(w, createProxyError, http.StatusInternalServerError) return } for name, values := range r.Header { @@ -50,7 +50,7 @@ func runProxy(g *golim) func(w http.ResponseWriter, r *http.Request) { resp, err := customTransport.RoundTrip(proxyReq) if err != nil { - http.Error(w, "Error sending proxy request", http.StatusInternalServerError) + http.Error(w, sendingProxyError, http.StatusInternalServerError) return } defer resp.Body.Close()