From 564bf8d17e4ee7b414c6ccb94d7023fe5b54b1bf Mon Sep 17 00:00:00 2001 From: Hitoshi Mitake Date: Wed, 20 Jul 2016 11:27:15 +0900 Subject: [PATCH] client: utility functions for getting detail of v2 auth errors 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 https://github.com/coreos/etcd/issues/5894 --- client/util.go | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/client/util.go b/client/util.go index 198bff9654d..15a8babff4d 100644 --- a/client/util.go +++ b/client/util.go @@ -14,6 +14,20 @@ 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 { @@ -21,3 +35,19 @@ func IsKeyNotFound(err error) bool { } 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 +}