Skip to content

Commit

Permalink
:humer: move the error message to const file
Browse files Browse the repository at this point in the history
  • Loading branch information
khalil committed Mar 28, 2024
1 parent 1f2ad1c commit 14b1667
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
11 changes: 11 additions & 0 deletions const.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
10 changes: 5 additions & 5 deletions golim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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 {
Expand All @@ -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()
Expand Down

0 comments on commit 14b1667

Please sign in to comment.