Debian Codegraph @

vendor/github.com/coreos/etcd/client/util.go

raw
1 // Copyright 2016 The etcd Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package client
16
17 import (
18 "regexp"
19 )
20
21 var (
22 roleNotFoundRegExp *regexp.Regexp
23 userNotFoundRegExp *regexp.Regexp
24 )
25
26 func init() {
27 roleNotFoundRegExp = regexp.MustCompile("auth: Role .* does not exist.")
28 userNotFoundRegExp = regexp.MustCompile("auth: User .* does not exist.")
29 }
30
31 // IsKeyNotFound returns true if the error code is ErrorCodeKeyNotFound.
32 func IsKeyNotFound(err error) bool {
33 if cErr, ok := err.(Error); ok {
34 return cErr.Code == ErrorCodeKeyNotFound
35 }
36 return false
37 }
38
39 // IsRoleNotFound returns true if the error means role not found of v2 API.
40 func IsRoleNotFound(err error) bool {
41 if ae, ok := err.(authError); ok {
42 return roleNotFoundRegExp.MatchString(ae.Message)
43 }
44 return false
45 }
46
47 // IsUserNotFound returns true if the error means user not found of v2 API.
48 func IsUserNotFound(err error) bool {
49 if ae, ok := err.(authError); ok {
50 return userNotFoundRegExp.MatchString(ae.Message)
51 }
52 return false
53 }