Skip to content

Commit

Permalink
Rename Error to Err
Browse files Browse the repository at this point in the history
  • Loading branch information
xorkevin committed Mar 26, 2023
1 parent 463a8cb commit ed076d2
Show file tree
Hide file tree
Showing 76 changed files with 718 additions and 712 deletions.
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ func (c *Client) Setup(ctx context.Context, secret string) (*ResSetup, error) {
return nil, err
}
if !decoded {
return nil, kerrors.WithKind(nil, ErrorServerRes, "Non-decodable response")
return nil, kerrors.WithKind(nil, ErrServerRes, "Non-decodable response")
}
c.log.Info(ctx, "Successfully setup governor", klog.AString("version", body.Version))
return body, nil
Expand Down
6 changes: 3 additions & 3 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (c *testClientC) echo(args []string) error {
if err != nil {
return kerrors.WithMsg(err, "Could not read file")
}
if err := c.term.WriteFile("testoutput.txt", f, 0644); err != nil {
if err := c.term.WriteFile("testoutput.txt", f, 0o644); err != nil {
return kerrors.WithMsg(err, "Could not write file")
}

Expand Down Expand Up @@ -281,7 +281,7 @@ servicec:
Fsys: fstest.MapFS{
"test.txt": &fstest.MapFile{
Data: []byte(`test file contents`),
Mode: 0644,
Mode: 0o644,
ModTime: time.Now(),
},
},
Expand Down Expand Up @@ -333,7 +333,7 @@ servicec:

err = clientC.fail(nil)
assert.Error(err)
assert.ErrorIs(err, ErrorServerRes)
assert.ErrorIs(err, ErrServerRes)
var kerr *kerrors.Error
assert.ErrorAs(err, &kerr)
assert.Equal("Test fail", kerr.Message)
Expand Down
6 changes: 3 additions & 3 deletions clientconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,11 @@ func (s *clientSettings) init(flags ClientFlags) error {
}
if s.configReader != nil {
if err := s.v.ReadConfig(s.configReader); err != nil {
return kerrors.WithKind(err, ErrorInvalidConfig, "Failed to read in config")
return kerrors.WithKind(err, ErrInvalidConfig, "Failed to read in config")
}
} else {
if err := s.v.ReadInConfig(); err != nil {
return kerrors.WithKind(err, ErrorInvalidConfig, "Failed to read in config")
return kerrors.WithKind(err, ErrInvalidConfig, "Failed to read in config")
}
}

Expand All @@ -79,7 +79,7 @@ func (s *clientSettings) init(flags ClientFlags) error {
if t, err := time.ParseDuration(s.v.GetString("http.timeout")); err == nil {
s.httpClient.timeout = t
} else {
return kerrors.WithKind(err, ErrorInvalidConfig, "Invalid http client timeout")
return kerrors.WithKind(err, ErrInvalidConfig, "Invalid http client timeout")
}

return nil
Expand Down
36 changes: 18 additions & 18 deletions clienthttp.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,29 +38,29 @@ type (

// Client http errors
var (
// ErrorInvalidClientReq is returned when the client request could not be made
ErrorInvalidClientReq errorInvalidClientReq
// ErrorInvalidServerRes is returned on an invalid server response
ErrorInvalidServerRes errorInvalidServerRes
// ErrorServerRes is a returned server error
ErrorServerRes errorServerRes
// ErrInvalidClientReq is returned when the client request could not be made
ErrInvalidClientReq errInvalidClientReq
// ErrInvalidServerRes is returned on an invalid server response
ErrInvalidServerRes errInvalidServerRes
// ErrServerRes is a returned server error
ErrServerRes errServerRes
)

type (
errorInvalidClientReq struct{}
errorInvalidServerRes struct{}
errorServerRes struct{}
errInvalidClientReq struct{}
errInvalidServerRes struct{}
errServerRes struct{}
)

func (e errorInvalidClientReq) Error() string {
func (e errInvalidClientReq) Error() string {
return "Invalid client request"
}

func (e errorInvalidServerRes) Error() string {
func (e errInvalidServerRes) Error() string {
return "Invalid server response"
}

func (e errorServerRes) Error() string {
func (e errServerRes) Error() string {
return "Error server response"
}

Expand All @@ -85,7 +85,7 @@ func (c *httpClient) subclient(path string, l klog.Logger) HTTPClient {
func (c *httpClient) Req(method, path string, body io.Reader) (*http.Request, error) {
req, err := http.NewRequest(method, c.base+path, body)
if err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidClientReq, "Malformed request")
return nil, kerrors.WithKind(err, ErrInvalidClientReq, "Malformed request")
}
return req, nil
}
Expand All @@ -95,7 +95,7 @@ func (c *httpClient) Do(ctx context.Context, r *http.Request) (_ *http.Response,
ctx = klog.CtxWithAttrs(ctx, klog.AString("gov.httpc.url", r.URL.String()))
res, err := c.httpc.Do(r)
if err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidClientReq, "Failed request")
return nil, kerrors.WithKind(err, ErrInvalidClientReq, "Failed request")
}
if res.StatusCode >= http.StatusBadRequest {
defer func() {
Expand All @@ -110,9 +110,9 @@ func (c *httpClient) Do(ctx context.Context, r *http.Request) (_ *http.Response,
}()
var errres ErrorRes
if err := json.NewDecoder(res.Body).Decode(&errres); err != nil {
return res, kerrors.WithKind(err, ErrorInvalidServerRes, "Failed decoding response")
return res, kerrors.WithKind(err, ErrInvalidServerRes, "Failed decoding response")
}
return res, kerrors.WithKind(nil, ErrorServerRes, errres.Message)
return res, kerrors.WithKind(nil, ErrServerRes, errres.Message)
}
return res, nil
}
Expand Down Expand Up @@ -145,7 +145,7 @@ func (c *HTTPFetcher) Do(ctx context.Context, r *http.Request) (*http.Response,
func (c *HTTPFetcher) ReqJSON(method, path string, data interface{}) (*http.Request, error) {
b, err := kjson.Marshal(data)
if err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidClientReq, "Failed to encode body to json")
return nil, kerrors.WithKind(err, ErrInvalidClientReq, "Failed to encode body to json")
}
body := bytes.NewReader(b)
req, err := c.Req(method, path, body)
Expand Down Expand Up @@ -197,7 +197,7 @@ func (c *HTTPFetcher) DoJSON(ctx context.Context, r *http.Request, response inte
decoded := false
if response != nil && isStatusDecodable(res.StatusCode) {
if err := json.NewDecoder(res.Body).Decode(response); err != nil {
return res, false, kerrors.WithKind(err, ErrorInvalidServerRes, "Failed decoding response")
return res, false, kerrors.WithKind(err, ErrInvalidServerRes, "Failed decoding response")
}
decoded = true
}
Expand Down
50 changes: 25 additions & 25 deletions govconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,22 +222,22 @@ func newSettings(opts Opts) *settings {

// Config errors
var (
// ErrorInvalidConfig is returned when the config is invalid
ErrorInvalidConfig errorInvalidConfig
// ErrorVault is returned when failing to contact vault
ErrorVault errorVault
// ErrInvalidConfig is returned when the config is invalid
ErrInvalidConfig errInvalidConfig
// ErrVault is returned when failing to contact vault
ErrVault errVault
)

type (
errorInvalidConfig struct{}
errorVault struct{}
errInvalidConfig struct{}
errVault struct{}
)

func (e errorInvalidConfig) Error() string {
func (e errInvalidConfig) Error() string {
return "Invalid config"
}

func (e errorVault) Error() string {
func (e errVault) Error() string {
return "Failed vault request"
}

Expand All @@ -263,11 +263,11 @@ func (s *settings) init(ctx context.Context, flags Flags) error {

if s.configReader != nil {
if err := s.v.ReadConfig(s.configReader); err != nil {
return kerrors.WithKind(err, ErrorInvalidConfig, "Failed to read in config")
return kerrors.WithKind(err, ErrInvalidConfig, "Failed to read in config")
}
} else {
if err := s.v.ReadInConfig(); err != nil {
return kerrors.WithKind(err, ErrorInvalidConfig, "Failed to read in config")
return kerrors.WithKind(err, ErrInvalidConfig, "Failed to read in config")
}
}

Expand Down Expand Up @@ -323,19 +323,19 @@ func newSecretsFileSource(s string, r io.Reader) (secretsClient, error) {
var err error
b, err = io.ReadAll(r)
if err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidConfig, "Failed to read secrets file source")
return nil, kerrors.WithKind(err, ErrInvalidConfig, "Failed to read secrets file source")
}
s = "io.Reader"
} else {
var err error
b, err = os.ReadFile(s)
if err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidConfig, "Failed to read secrets file source")
return nil, kerrors.WithKind(err, ErrInvalidConfig, "Failed to read secrets file source")
}
}
data := secretsFileData{}
if err := yaml.Unmarshal(b, &data); err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidConfig, "Invalid secrets file source file")
return nil, kerrors.WithKind(err, ErrInvalidConfig, "Invalid secrets file source file")
}
return &secretsFileSource{
path: s,
Expand All @@ -354,7 +354,7 @@ func (s *secretsFileSource) Init(ctx context.Context) error {
func (s *secretsFileSource) GetSecret(ctx context.Context, kvpath string) (map[string]interface{}, time.Time, error) {
data, ok := s.data.Data[kvpath]
if !ok {
return nil, time.Time{}, kerrors.WithKind(nil, ErrorVault, "Failed to read vault secret")
return nil, time.Time{}, kerrors.WithKind(nil, ErrVault, "Failed to read vault secret")
}
return data, time.Time{}, nil
}
Expand Down Expand Up @@ -383,12 +383,12 @@ type (
func newSecretsVaultSource(config secretsVaultSourceConfig) (secretsClient, error) {
vconfig := vaultapi.DefaultConfig()
if err := vconfig.Error; err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidConfig, "Failed to create vault default config")
return nil, kerrors.WithKind(err, ErrInvalidConfig, "Failed to create vault default config")
}
vconfig.Address = config.Addr
vault, err := vaultapi.NewClient(vconfig)
if err != nil {
return nil, kerrors.WithKind(err, ErrorInvalidConfig, "Failed to create vault client")
return nil, kerrors.WithKind(err, ErrInvalidConfig, "Failed to create vault client")
}
return &secretsVaultSource{
address: config.Addr,
Expand Down Expand Up @@ -439,7 +439,7 @@ func (s *secretsVaultSource) authVault(ctx context.Context) error {
"role": s.config.K8SRole,
})
if err != nil {
return kerrors.WithKind(err, ErrorVault, "Failed to auth with vault k8s")
return kerrors.WithKind(err, ErrVault, "Failed to auth with vault k8s")
}
s.vaultExpire = time.Now().Round(0).Add(time.Duration(authsecret.Auth.LeaseDuration) * time.Second)
s.vault.SetToken(authsecret.Auth.ClientToken)
Expand All @@ -453,7 +453,7 @@ func (s *secretsVaultSource) GetSecret(ctx context.Context, kvpath string) (map[

secret, err := s.vault.Logical().ReadWithContext(ctx, kvpath)
if err != nil {
return nil, time.Time{}, kerrors.WithKind(err, ErrorVault, "Failed to read vault secret")
return nil, time.Time{}, kerrors.WithKind(err, ErrVault, "Failed to read vault secret")
}
data := secret.Data
if v, ok := data["data"].(map[string]interface{}); ok {
Expand Down Expand Up @@ -492,17 +492,17 @@ func (s *settings) initsecrets(ctx context.Context) error {
config.K8SLoginPath = s.v.GetString("vault.k8s.loginpath")
jwtpath := s.v.GetString("vault.k8s.jwtpath")
if config.K8SRole == "" {
return kerrors.WithKind(nil, ErrorInvalidConfig, "No vault role set")
return kerrors.WithKind(nil, ErrInvalidConfig, "No vault role set")
}
if config.K8SLoginPath == "" {
return kerrors.WithKind(nil, ErrorInvalidConfig, "No vault k8s login path set")
return kerrors.WithKind(nil, ErrInvalidConfig, "No vault k8s login path set")
}
if jwtpath == "" {
return kerrors.WithKind(nil, ErrorInvalidConfig, "No path for vault k8s service account jwt auth")
return kerrors.WithKind(nil, ErrInvalidConfig, "No path for vault k8s service account jwt auth")
}
jwtbytes, err := os.ReadFile(jwtpath)
if err != nil {
return kerrors.WithKind(err, ErrorInvalidConfig, "Failed to read vault k8s service account jwt")
return kerrors.WithKind(err, ErrInvalidConfig, "Failed to read vault k8s service account jwt")
}
config.K8SJWT = string(jwtbytes)
}
Expand All @@ -522,15 +522,15 @@ func (s *settings) getSecret(ctx context.Context, key string, cacheDuration time
s := v.(vaultSecret)
if s.isValid() {
if err := mapstructure.Decode(s.value, target); err != nil {
return kerrors.WithKind(err, ErrorInvalidConfig, "Failed decoding secret")
return kerrors.WithKind(err, ErrInvalidConfig, "Failed decoding secret")
}
return nil
}
}

kvpath := s.v.GetString(key)
if kvpath == "" {
return kerrors.WithKind(nil, ErrorInvalidConfig, "Empty secret key "+key)
return kerrors.WithKind(nil, ErrInvalidConfig, "Empty secret key "+key)
}

data, expire, err := s.vault.GetSecret(ctx, kvpath)
Expand All @@ -547,7 +547,7 @@ func (s *settings) getSecret(ctx context.Context, key string, cacheDuration time
})

if err := mapstructure.Decode(data, target); err != nil {
return kerrors.WithKind(err, ErrorInvalidConfig, "Failed decoding secret")
return kerrors.WithKind(err, ErrInvalidConfig, "Failed decoding secret")
}
return nil
}
Expand Down
4 changes: 2 additions & 2 deletions govcontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ func (c *Context) WriteError(err error) {
}
}

if !errors.Is(err, ErrorNoLog) {
if !errors.Is(err, ErrNoLog) {
if rerr.Status >= http.StatusBadRequest && rerr.Status < http.StatusInternalServerError {
c.log.WarnErr(c.Ctx(), err)
} else {
Expand Down Expand Up @@ -431,7 +431,7 @@ func (w *Websocket) CloseError(err error) {
}
}

if !errors.Is(err, ErrorNoLog) {
if !errors.Is(err, ErrNoLog) {
if werr.Status != int(websocket.StatusInternalError) {
w.c.log.WarnErr(w.c.Ctx(), err)
} else {
Expand Down
20 changes: 10 additions & 10 deletions goverror.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@ import (

// Sentinel errors
var (
// ErrorNoLog is an error kind to prevent logging
ErrorNoLog errorNoLog
// ErrorUnreachable is an error kind to mark unreachable code
ErrorUnreachable errorUnreachable
// ErrNoLog is an error kind to prevent logging
ErrNoLog errNoLog
// ErrUnreachable is an error kind to mark unreachable code
ErrUnreachable errUnreachable
)

type (
errorNoLog struct{}
errNoLog struct{}
)

func (e errorNoLog) Error() string {
func (e errNoLog) Error() string {
return "No log"
}

Expand Down Expand Up @@ -57,10 +57,10 @@ func (e *ErrorRes) Error() string {
}

type (
errorUnreachable struct{}
errUnreachable struct{}
)

func (e errorUnreachable) Error() string {
func (e errUnreachable) Error() string {
return "Unreachable code. Invariant violated"
}

Expand Down Expand Up @@ -89,7 +89,7 @@ func (e *ErrorTooManyRequests) RetryAfterTime() string {
func ErrWithNoLog(err error) error {
return kerrors.New(
kerrors.OptMsg("No log"),
kerrors.OptKind(ErrorNoLog),
kerrors.OptKind(ErrNoLog),
kerrors.OptInner(err),
kerrors.OptSkip(1),
)
Expand All @@ -113,7 +113,7 @@ func ErrWithRes(err error, status int, code string, resmsg string) error {
func ErrWithUnreachable(err error, msg string) error {
return kerrors.New(
kerrors.OptMsg(msg),
kerrors.OptKind(ErrorUnreachable),
kerrors.OptKind(ErrUnreachable),
kerrors.OptInner(err),
kerrors.OptSkip(1),
)
Expand Down
Loading

0 comments on commit ed076d2

Please sign in to comment.