diff --git a/http/bucket_service.go b/http/bucket_service.go index e44ee9f56a..895b8e2707 100644 --- a/http/bucket_service.go +++ b/http/bucket_service.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "net/http" "path" @@ -310,7 +309,7 @@ func (s *BucketService) FindBucket(ctx context.Context, filter platform.BucketFi } if n == 0 { - return nil, errors.New("found no matching buckets") + return nil, ErrNotFound } return bs[0], nil diff --git a/http/errors.go b/http/errors.go index 4091c5dd2a..d7da62c8db 100644 --- a/http/errors.go +++ b/http/errors.go @@ -16,6 +16,9 @@ const ( errorHeaderMaxLength = 256 ) +// ErrNotFound is returned when a request for a resource returns no results. +var ErrNotFound = errors.New("no results found") + // AuthzError is returned for authorization errors. When this error type is returned, // the user can be presented with a generic "authorization failed" error, but // the system can log the underlying AuthzError() so that operators have insight diff --git a/http/org_service.go b/http/org_service.go index 847a2f7583..37368502e2 100644 --- a/http/org_service.go +++ b/http/org_service.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "net/http" "path" @@ -271,8 +270,8 @@ func (s *OrganizationService) FindOrganization(ctx context.Context, filter platf return nil, err } - if n < 1 { - return nil, errors.New("expected at least one organization") + if n == 0 { + return nil, ErrNotFound } return os[0], nil diff --git a/http/user_service.go b/http/user_service.go index 6730a883d9..62181256cd 100644 --- a/http/user_service.go +++ b/http/user_service.go @@ -4,7 +4,6 @@ import ( "bytes" "context" "encoding/json" - "errors" "net/http" "path" @@ -294,7 +293,7 @@ func (s *UserService) FindUser(ctx context.Context, filter platform.UserFilter) } if n == 0 { - return nil, errors.New("found no matching user") + return nil, ErrNotFound } return users[0], nil diff --git a/http/write_handler.go b/http/write_handler.go index 8bcd7e5d12..dec37b8363 100644 --- a/http/write_handler.go +++ b/http/write_handler.go @@ -78,8 +78,12 @@ func (h *WriteHandler) handleWrite(w http.ResponseWriter, r *http.Request) { var org *platform.Organization if id, err := platform.IDFromString(req.Org); err == nil { // Decoded ID successfully. Make sure it's a real org. - if o, err := h.OrganizationService.FindOrganizationByID(ctx, *id); err == nil { + o, err := h.OrganizationService.FindOrganizationByID(ctx, *id) + if err == nil { org = o + } else if err != ErrNotFound { + EncodeError(ctx, err, w) + return } } if org == nil { @@ -96,11 +100,15 @@ func (h *WriteHandler) handleWrite(w http.ResponseWriter, r *http.Request) { var bucket *platform.Bucket if id, err := platform.IDFromString(req.Bucket); err == nil { // Decoded ID successfully. Make sure it's a real bucket. - if b, err := h.BucketService.FindBucket(ctx, platform.BucketFilter{ + b, err := h.BucketService.FindBucket(ctx, platform.BucketFilter{ OrganizationID: &org.ID, ID: id, - }); err == nil { + }) + if err == nil { bucket = b + } else if err != ErrNotFound { + EncodeError(ctx, err, w) + return } }