Skip to content
This repository has been archived by the owner on Feb 16, 2023. It is now read-only.

Commit

Permalink
Merge pull request #172 from secrethub/feature/improved-secret-not-fo…
Browse files Browse the repository at this point in the history
…und-error

Update secret not found error message
  • Loading branch information
SimonBarendse authored Mar 18, 2020
2 parents 34f204d + 24b8472 commit 340f69a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
6 changes: 4 additions & 2 deletions internals/api/server_errors.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"errors"
"net/http"

"fmt"
Expand Down Expand Up @@ -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
}
22 changes: 19 additions & 3 deletions pkg/secrethub/secret_version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package secrethub

import (
"fmt"

"github.com/secrethub/secrethub-go/pkg/secrethub/iterator"

units "github.com/docker/go-units"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand Down

0 comments on commit 340f69a

Please sign in to comment.