Skip to content

Commit

Permalink
fix: use switch type
Browse files Browse the repository at this point in the history
  • Loading branch information
Claude-Zq committed Jul 30, 2024
1 parent feb5a2b commit 4d5ac18
Showing 1 changed file with 14 additions and 7 deletions.
21 changes: 14 additions & 7 deletions middleware/csrf/custom_errorfunc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,21 @@ func myErrFunc(_ context.Context, ctx *app.RequestContext) {
return
}

if 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
} else if errors.Is(err, errMissingSalt) {
fmt.Println(err.Error())
ctx.String(http.StatusInternalServerError, err.Error()) // get salt failed, which is unexpected
} else if errors.Is(err, errInvalidToken) {
ctx.String(http.StatusBadRequest, err.Error()) // csrf-token is invalid
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 4d5ac18

Please sign in to comment.