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

feat: handle validation error #107

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open

Conversation

mukezhz
Copy link
Member

@mukezhz mukezhz commented Apr 23, 2024

No description provided.

Comment on lines +36 to +52
func HandleValidationError(logger framework.Logger, c *gin.Context, err error) {
logger.Error("VALIDATION ERROR:", err)
var ve validator.ValidationErrors
if errors.As(err, &ve) {
out := make([]ErrorMsg, len(ve))
for i, fe := range ve {
out[i] = ErrorMsg{
fe.Field(),
fe.Namespace(),
getErrorMsg(fe),
}
}
ErrorJSON(c, http.StatusBadRequest, out)
} else {
ErrorJSON(c, http.StatusBadRequest, err.Error())
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good implementation.
How about doing it automatically in HandleError function in case we have validation error so that we don't need to pass different error handlers everytime.
What do you think? @mukezhz

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dipeshdulal thank you for the suggestion

that would be awesome. I will change the code.

🙇‍♂️

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

Successfully merging this pull request may close these issues.

None yet

2 participants