Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the example:middleware/csrf/custom_errorfunc/main.go,the function myErrFunc does not implement what the author wants. #151

Open
Tracy-coder opened this issue Jul 13, 2024 · 1 comment

Comments

@Tracy-coder
Copy link

In the example:middleware/csrf/custom_errorfunc/main.go,the function myErrFunc does not implement what the author wants.

var (
errMissingHeader = errors.New("[CSRF] missing csrf token in header")
errMissingQuery = errors.New("[CSRF] missing csrf token in query")
errMissingParam = errors.New("[CSRF] missing csrf token in param")
errMissingForm = errors.New("[CSRF] missing csrf token in form")
errMissingSalt = errors.New("[CSRF] missing salt")
errInvalidToken = errors.New("[CSRF] invalid token")
)

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
}
ctx.Abort()
}

The switch statement can't be used for error handling in this way, which may be misleading for those reading this example.

@Claude-Zq
Copy link
Contributor

Thanks for the reminder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants