Skip to content

Commit

Permalink
client: utility functions for getting detail of v2 auth errors
Browse files Browse the repository at this point in the history
Current v2 auth API doesn't propagate its error code. This commit adds
utility functions for parsing error messages and getting detail of v2
auth errors.

Fixes etcd-io#5894
  • Loading branch information
mitake committed Jul 31, 2016
1 parent a61862a commit 564bf8d
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions client/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,40 @@

package client

import (
"regexp"
)

var (
roleNotFoundRegExp *regexp.Regexp
userNotFoundRegExp *regexp.Regexp
)

func init() {
roleNotFoundRegExp = regexp.MustCompile("auth: Role .* does not exist.")
userNotFoundRegExp = regexp.MustCompile("auth: User .* does not exist.")
}

// IsKeyNotFound returns true if the error code is ErrorCodeKeyNotFound.
func IsKeyNotFound(err error) bool {
if cErr, ok := err.(Error); ok {
return cErr.Code == ErrorCodeKeyNotFound
}
return false
}

// IsRoleNotFound returns true if the error means role not found of v2 API.
func IsRoleNotFound(err error) bool {
if ae, ok := err.(authError); ok {
return roleNotFoundRegExp.MatchString(ae.Message)
}
return false
}

// IsUserNotFound returns true if the error means user not found of v2 API.
func IsUserNotFound(err error) bool {
if ae, ok := err.(authError); ok {
return userNotFoundRegExp.MatchString(ae.Message)
}
return false
}

0 comments on commit 564bf8d

Please sign in to comment.