Skip to content

Commit

Permalink
fix: fix misuse of switch (#152)
Browse files Browse the repository at this point in the history
* fix: fix misuse of switch

* fix: use switch type
  • Loading branch information
Claude-Zq authored Jan 14, 2025
1 parent 48f44c5 commit f786059
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions middleware/csrf/custom_errorfunc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,25 @@ var (
// myErrFunc is executed when an error occurs in csrf middleware.
func myErrFunc(_ context.Context, ctx *app.RequestContext) {
err := ctx.Errors.Last()
switch err {
case errMissingForm, errMissingParam, errMissingHeader, errMissingQuery:
ctx.String(http.StatusBadRequest, err.Error()) // extract csrf-token failed
case errMissingSalt:
fmt.Println(err.Error())
ctx.String(http.StatusInternalServerError, err.Error()) // get salt failed,which is unexpected
case errInvalidToken:
ctx.String(http.StatusBadRequest, err.Error()) // csrf-token is invalid
if err == nil {
return
}

switch err.Err.(type) {
case error:
switch {
case errors.Is(err, errMissingForm), errors.Is(err, errMissingParam), errors.Is(err, errMissingHeader), errors.Is(err, errMissingQuery):
ctx.String(http.StatusBadRequest, err.Error()) // extract csrf-token failed
case errors.Is(err, errMissingSalt):
fmt.Println(err.Error())
ctx.String(http.StatusInternalServerError, err.Error()) // get salt failed, which is unexpected
case errors.Is(err, errInvalidToken):
ctx.String(http.StatusBadRequest, err.Error()) // csrf-token is invalid
default:
ctx.String(http.StatusInternalServerError, "Unknown error") // handle unknown errors
}
}

ctx.Abort()
}

Expand Down

0 comments on commit f786059

Please sign in to comment.