satellite/metabase: custom error for commit object

We stoped returning lots of errors as is to avoid leaking our internals
but some errors were meanigful for client. Example of such error is
"exceeded maximum number of parts". With this change we are wrapping
some important commit object errors with new ErrFailedPrecondition
error to be able to return it easily to uplink.

Change-Id: Id834b78362ed1920f0c3f6f1c7d9587bfd27e36a
This commit is contained in:
Michal Niewrzal 2023-09-13 15:30:32 +02:00 committed by Michał Niewrzał
parent f4fe983b1e
commit 975d953cb8
3 changed files with 9 additions and 5 deletions

View File

@ -30,6 +30,8 @@ var (
ErrObjectNotFound = errs.Class("object not found") ErrObjectNotFound = errs.Class("object not found")
// ErrInvalidRequest is used to indicate invalid requests. // ErrInvalidRequest is used to indicate invalid requests.
ErrInvalidRequest = errs.Class("metabase: invalid request") ErrInvalidRequest = errs.Class("metabase: invalid request")
// ErrFailedPrecondition is used to indicate that some conditions in the request has failed.
ErrFailedPrecondition = errs.Class("metabase: failed precondition")
// ErrConflict is used to indicate conflict with the request. // ErrConflict is used to indicate conflict with the request.
ErrConflict = errs.Class("metabase: conflict") ErrConflict = errs.Class("metabase: conflict")
) )
@ -931,7 +933,7 @@ func (db *DB) validateParts(segments []segmentInfoForCommit) error {
} }
if len(partSize) > db.config.MaxNumberOfParts { if len(partSize) > db.config.MaxNumberOfParts {
return Error.New("exceeded maximum number of parts: %d", db.config.MaxNumberOfParts) return ErrFailedPrecondition.New("exceeded maximum number of parts: %d", db.config.MaxNumberOfParts)
} }
for part, size := range partSize { for part, size := range partSize {
@ -941,7 +943,7 @@ func (db *DB) validateParts(segments []segmentInfoForCommit) error {
} }
if size < db.config.MinPartSize { if size < db.config.MinPartSize {
return Error.New("size of part number %d is below minimum threshold, got: %s, min: %s", part, size, db.config.MinPartSize) return ErrFailedPrecondition.New("size of part number %d is below minimum threshold, got: %s, min: %s", part, size, db.config.MinPartSize)
} }
} }

View File

@ -4455,7 +4455,7 @@ func TestCommitObjectWithIncorrectPartSize(t *testing.T) {
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: obj, ObjectStream: obj,
}, },
ErrClass: &metabase.Error, ErrClass: &metabase.ErrFailedPrecondition,
ErrText: "size of part number 0 is below minimum threshold, got: 2.0 MiB, min: 5.0 MiB", ErrText: "size of part number 0 is below minimum threshold, got: 2.0 MiB, min: 5.0 MiB",
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -4656,7 +4656,7 @@ func TestCommitObjectWithIncorrectPartSize(t *testing.T) {
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: obj, ObjectStream: obj,
}, },
ErrClass: &metabase.Error, ErrClass: &metabase.ErrFailedPrecondition,
ErrText: "size of part number 2 is below minimum threshold, got: 1.0 MiB, min: 5.0 MiB", ErrText: "size of part number 2 is below minimum threshold, got: 1.0 MiB, min: 5.0 MiB",
}.Check(ctx, t, db) }.Check(ctx, t, db)
@ -4793,7 +4793,7 @@ func TestCommitObjectWithIncorrectAmountOfParts(t *testing.T) {
Opts: metabase.CommitObject{ Opts: metabase.CommitObject{
ObjectStream: obj, ObjectStream: obj,
}, },
ErrClass: &metabase.Error, ErrClass: &metabase.ErrFailedPrecondition,
ErrText: "exceeded maximum number of parts: 3", ErrText: "exceeded maximum number of parts: 3",
}.Check(ctx, t, db) }.Check(ctx, t, db)

View File

@ -321,6 +321,8 @@ func (endpoint *Endpoint) convertMetabaseErr(err error) error {
return rpcstatus.Error(rpcstatus.NotFound, "segment not found: "+message) return rpcstatus.Error(rpcstatus.NotFound, "segment not found: "+message)
case metabase.ErrInvalidRequest.Has(err): case metabase.ErrInvalidRequest.Has(err):
return rpcstatus.Error(rpcstatus.InvalidArgument, err.Error()) return rpcstatus.Error(rpcstatus.InvalidArgument, err.Error())
case metabase.ErrFailedPrecondition.Has(err):
return rpcstatus.Error(rpcstatus.FailedPrecondition, err.Error())
case metabase.ErrObjectAlreadyExists.Has(err): case metabase.ErrObjectAlreadyExists.Has(err):
return rpcstatus.Error(rpcstatus.AlreadyExists, err.Error()) return rpcstatus.Error(rpcstatus.AlreadyExists, err.Error())
case metabase.ErrPendingObjectMissing.Has(err): case metabase.ErrPendingObjectMissing.Has(err):