Added custom error for could not get User ID from JWT
This commit is contained in:
parent
c8da860eab
commit
a4b8a44e47
|
@ -104,4 +104,16 @@ func (err ErrBookTitleCannotBeEmpty) Error() string {
|
||||||
return fmt.Sprintf("the book should at least have a title")
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -87,7 +87,7 @@ func GetCurrentUser(c echo.Context) (user User, err error) {
|
||||||
claims := jwtinf.Claims.(jwt.MapClaims)
|
claims := jwtinf.Claims.(jwt.MapClaims)
|
||||||
userID, ok := claims["id"].(float64)
|
userID, ok := claims["id"].(float64)
|
||||||
if !ok {
|
if !ok {
|
||||||
return user, fmt.Errorf("Error getting UserID")
|
return user, ErrCouldNotGetUserID{}
|
||||||
}
|
}
|
||||||
user = User{
|
user = User{
|
||||||
ID: int64(userID),
|
ID: int64(userID),
|
||||||
|
|
Loading…
Reference in New Issue