-
Notifications
You must be signed in to change notification settings - Fork 7
/
errors.go
122 lines (105 loc) · 4.84 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package cmd
import (
"context"
"fmt"
"net"
"net/url"
"strings"
"github.com/nerdalize/nerd/pkg/kubevisor"
transferstore "github.com/nerdalize/nerd/pkg/transfer/store"
"github.com/nerdalize/nerd/svc"
"github.com/pkg/errors"
)
var (
// ErrNamespaceNotSet is returned when no namespace config is found in the kube config file.
ErrNamespaceNotSet = errors.New("no cluster set, use `nerd login` to update your configuration")
// ErrNotLoggedIn is returned when no oauth access token was found in the config file.
ErrNotLoggedIn = errors.New("you're not logged in. Please login with `nerd login`")
// ErrOverwriteWarning is returned when a user is trying to use the same name for input and output datasets
ErrOverwriteWarning = errors.New("it is not possible to use the same name for input and output datasets, as it would overwrite your dataset")
)
// errShowHelp can be returned by commands to show the commands help message next to the error.
type errShowHelp string
func (e errShowHelp) Error() string { return string(e) }
// errShowUsage can be returned by commands to show usage.
type errShowUsage string
func (e errShowUsage) Error() string { return string(e) }
func renderServiceError(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
switch err := err.(type) {
case net.Error:
return errors.Errorf("%s: please check your internet connection, and try again.", fmt.Errorf(format, args...))
case *url.Error:
if err, ok := err.Err.(net.Error); ok && err.Timeout() {
return errors.Errorf("%s:please check your internet connection, and try again.", fmt.Errorf(format, args...))
}
}
err = errors.Cause(err)
if err == context.Canceled {
return errors.Errorf("%s: process was canceled", fmt.Errorf(format, args...))
}
switch {
case strings.Contains(err.Error(), "failed to refresh token"):
return errors.Errorf("Please check your permissions or try to login again.")
case kubevisor.IsInvalidNameErr(err):
return errors.Errorf("%s: invalid name, must consist of alphanumeric characters, '-' or '.'", fmt.Errorf(format, args...))
case kubevisor.IsDeadlineErr(err):
return errors.Errorf("%s: action took to long to complete, try again or check your internet connection", fmt.Errorf(format, args...))
case kubevisor.IsNetworkErr(err):
return errors.Errorf("%s: failed to reach the cluster, make sure you're connected to the internet and try again. Also, you can check the status page: http://status.nerdalize.com/", fmt.Errorf(format, args...))
case kubevisor.IsNotExistsErr(err):
return errors.Errorf("%s: it does not exist", fmt.Errorf(format, args...))
case kubevisor.IsKubernetesErr(err):
return errors.Errorf("%s: cluster failed to perform action: %v", fmt.Errorf(format, args...), err)
case kubevisor.IsAlreadyExistsErr(err):
return errors.Errorf("%s: this name is already in use, please use the list command to see what is already there", fmt.Errorf(format, args...))
case kubevisor.IsNamespaceNotExistsErr(err):
return errors.Errorf("%s: the namespace does not exist or you have no access. If the problem persists, please contact mayday@nerdalize.com.", fmt.Errorf(format, args...))
case kubevisor.IsServiceUnavailableErr(err):
return errors.Errorf("%s: cluster is currently unable to receive requests, try again later. Also, you can check the status page: http://status.nerdalize.com/", fmt.Errorf(format, args...))
case kubevisor.IsUnauthorizedErr(err):
return errors.Errorf("%s: you do not have permission to perform this action", fmt.Errorf(format, args...))
case svc.IsRaceConditionErr(err):
return errors.Errorf("%s: another process caused your action to fail, please try again", fmt.Errorf(format, args...))
case errors.Cause(err) == ErrNamespaceNotSet:
return ErrNamespaceNotSet
case errors.Cause(err) == ErrNotLoggedIn:
return ErrNotLoggedIn
case errors.Cause(err) == transferstore.ErrObjectNotExists:
return errors.Errorf("%s: dataset data is not available, it might still be uploading, check back again later", fmt.Errorf(format, args...))
default:
return errors.Wrapf(err, format, args...)
}
}
func renderConfigError(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
switch {
case errors.Cause(err) == ErrNotLoggedIn:
return ErrNotLoggedIn
case errors.Cause(err) == ErrNamespaceNotSet:
return ErrNamespaceNotSet
default:
return err
}
}
func renderClientError(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
err = errors.Cause(err)
switch err := err.(type) {
case net.Error:
return errors.Errorf("%s: please check your internet connection, and try again.", fmt.Errorf(format, args...))
case *url.Error:
if err, ok := err.Err.(net.Error); ok && err.Timeout() {
return errors.Errorf("%s:please check your internet connection, and try again.", fmt.Errorf(format, args...))
}
default:
return err
}
return err
}