Added custom error for could not get User ID from JWT

This commit is contained in:
konrad 2018-01-24 13:18:17 +01:00 committed by kolaente
parent c8da860eab
commit a4b8a44e47
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
2 changed files with 13 additions and 1 deletions

View File

@ -104,4 +104,16 @@ func (err ErrBookTitleCannotBeEmpty) Error() string {
return fmt.Sprintf("the book should at least have a title")
}
// ErrCouldNotGetUserID represents a "ErrCouldNotGetUserID" kind of error.
type ErrCouldNotGetUserID struct {}
// ErrBookTitleCannotBeEmpty checks if an error is a ErrBookTitleCannotBeEmpty.
func IsErrCouldNotGetUserID(err error) bool {
_, ok := err.(ErrCouldNotGetUserID)
return ok
}
func (err ErrCouldNotGetUserID) Error() string {
return fmt.Sprintf("could not get user ID")
}

View File

@ -87,7 +87,7 @@ func GetCurrentUser(c echo.Context) (user User, err error) {
claims := jwtinf.Claims.(jwt.MapClaims)
userID, ok := claims["id"].(float64)
if !ok {
return user, fmt.Errorf("Error getting UserID")
return user, ErrCouldNotGetUserID{}
}
user = User{
ID: int64(userID),