This repository has been archived by the owner on Mar 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
errors.go
69 lines (55 loc) · 2.01 KB
/
errors.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package clientcmd
import (
"strings"
kerrors "github.com/GoogleCloudPlatform/kubernetes/pkg/api/errors"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/clientcmd"
)
const (
unknownReason = 0
noServerFoundReason = 1
certificateAuthorityUnknownReason = 2
configurationInvalidReason = 3
certificateAuthorityUnknownMsg = "The server uses a certificate signed by unknown authority. You may need to use the --certificate-authority flag to provide the path to a certificate file for the certificate authority, or --insecure-skip-tls-verify to bypass the certificate check and use insecure connections."
notConfiguredMsg = `OpenShift is not configured. You need to run the login command in order to create a default config for your server and credentials:
osc login
You can also run this command again providing the path to a config file directly, either through the --config flag of the OPENSHIFTCONFIG environment variable.
`
)
func GetPrettyMessageFor(err error) string {
if err == nil {
return ""
}
reason := detectReason(err)
switch reason {
case noServerFoundReason:
return notConfiguredMsg
case certificateAuthorityUnknownReason:
return certificateAuthorityUnknownMsg
}
return err.Error()
}
func IsNoServerFound(err error) bool {
return detectReason(err) == noServerFoundReason
}
func IsConfigurationInvalid(err error) bool {
return detectReason(err) == configurationInvalidReason
}
func IsCertificateAuthorityUnknown(err error) bool {
return detectReason(err) == certificateAuthorityUnknownReason
}
func IsForbidden(err error) bool {
return kerrors.IsForbidden(err)
}
func detectReason(err error) int {
if err != nil {
switch {
case strings.Contains(err.Error(), "certificate signed by unknown authority"):
return certificateAuthorityUnknownReason
case strings.Contains(err.Error(), "no server defined"):
return noServerFoundReason
case clientcmd.IsConfigurationInvalid(err):
return configurationInvalidReason
}
}
return unknownReason
}