chore(http): return early if a critical error occurs in write path org/bucket lookup (#767)
parent
bbcbaee9fd
commit
43e0d143e7
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
@ -310,7 +309,7 @@ func (s *BucketService) FindBucket(ctx context.Context, filter platform.BucketFi
|
||||||
}
|
}
|
||||||
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return nil, errors.New("found no matching buckets")
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return bs[0], nil
|
return bs[0], nil
|
||||||
|
|
|
@ -16,6 +16,9 @@ const (
|
||||||
errorHeaderMaxLength = 256
|
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,
|
// 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 user can be presented with a generic "authorization failed" error, but
|
||||||
// the system can log the underlying AuthzError() so that operators have insight
|
// the system can log the underlying AuthzError() so that operators have insight
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
@ -271,8 +270,8 @@ func (s *OrganizationService) FindOrganization(ctx context.Context, filter platf
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
if n < 1 {
|
if n == 0 {
|
||||||
return nil, errors.New("expected at least one organization")
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return os[0], nil
|
return os[0], nil
|
||||||
|
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path"
|
"path"
|
||||||
|
|
||||||
|
@ -294,7 +293,7 @@ func (s *UserService) FindUser(ctx context.Context, filter platform.UserFilter)
|
||||||
}
|
}
|
||||||
|
|
||||||
if n == 0 {
|
if n == 0 {
|
||||||
return nil, errors.New("found no matching user")
|
return nil, ErrNotFound
|
||||||
}
|
}
|
||||||
|
|
||||||
return users[0], nil
|
return users[0], nil
|
||||||
|
|
|
@ -78,8 +78,12 @@ func (h *WriteHandler) handleWrite(w http.ResponseWriter, r *http.Request) {
|
||||||
var org *platform.Organization
|
var org *platform.Organization
|
||||||
if id, err := platform.IDFromString(req.Org); err == nil {
|
if id, err := platform.IDFromString(req.Org); err == nil {
|
||||||
// Decoded ID successfully. Make sure it's a real org.
|
// 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
|
org = o
|
||||||
|
} else if err != ErrNotFound {
|
||||||
|
EncodeError(ctx, err, w)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if org == nil {
|
if org == nil {
|
||||||
|
@ -96,11 +100,15 @@ func (h *WriteHandler) handleWrite(w http.ResponseWriter, r *http.Request) {
|
||||||
var bucket *platform.Bucket
|
var bucket *platform.Bucket
|
||||||
if id, err := platform.IDFromString(req.Bucket); err == nil {
|
if id, err := platform.IDFromString(req.Bucket); err == nil {
|
||||||
// Decoded ID successfully. Make sure it's a real bucket.
|
// 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,
|
OrganizationID: &org.ID,
|
||||||
ID: id,
|
ID: id,
|
||||||
}); err == nil {
|
})
|
||||||
|
if err == nil {
|
||||||
bucket = b
|
bucket = b
|
||||||
|
} else if err != ErrNotFound {
|
||||||
|
EncodeError(ctx, err, w)
|
||||||
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue