diff --git a/internals/api/server_errors.go b/internals/api/server_errors.go index 2908d7e3..15c633c6 100644 --- a/internals/api/server_errors.go +++ b/internals/api/server_errors.go @@ -1,6 +1,7 @@ package api import ( + "errors" "net/http" "fmt" @@ -104,9 +105,10 @@ var ( // IsErrNotFound returns whether the given error is caused by a un-existing resource. func IsErrNotFound(err error) bool { - statusError, ok := err.(errio.PublicStatusError) + var publicStatusError errio.PublicStatusError + ok := errors.As(err, &publicStatusError) if !ok { return false } - return statusError.StatusCode == 404 + return publicStatusError.StatusCode == 404 } diff --git a/pkg/secrethub/secret_version.go b/pkg/secrethub/secret_version.go index 8d13c827..1c1d2c1c 100644 --- a/pkg/secrethub/secret_version.go +++ b/pkg/secrethub/secret_version.go @@ -2,7 +2,6 @@ package secrethub import ( "fmt" - "github.com/secrethub/secrethub-go/pkg/secrethub/iterator" units "github.com/docker/go-units" @@ -23,6 +22,19 @@ var ( ErrCannotWriteToVersion = errClient.Code("cannot_write_version").Error("cannot (over)write a specific secret version, they are append only") ) +type errSecretNotFound struct { + path api.SecretPath + err error +} + +func (e *errSecretNotFound) Error() string { + return fmt.Sprintf("cannot find secret: \"%s\": %v", e.path, e.err) +} + +func (e *errSecretNotFound) Unwrap() error { + return e.err +} + // SecretVersionService handles operations on secret versions from SecretHub. type SecretVersionService interface { // GetWithData gets a secret version, with the sensitive data. @@ -80,7 +92,9 @@ func (s secretVersionService) Delete(path string) error { // get gets a version of a secret. withData specifies whether the encrypted data should be retrieved. func (s secretVersionService) get(path api.SecretPath, withData bool) (*api.SecretVersion, error) { blindName, err := s.client.convertPathToBlindName(path) - if err != nil { + if api.IsErrNotFound(err) { + return nil, &errSecretNotFound{path: path, err: err} + } else if err != nil { return nil, errio.Error(err) } @@ -95,7 +109,9 @@ func (s secretVersionService) get(path api.SecretPath, withData bool) (*api.Secr } encVersion, err := s.client.httpClient.GetSecretVersion(blindName, versionParam, withData) - if err != nil { + if api.IsErrNotFound(err) { + return nil, &errSecretNotFound{path: path, err: err} + } else if err != nil { return nil, errio.Error(err) }