From 09f5421bd7320509d536cc7d832f55f8b71d6538 Mon Sep 17 00:00:00 2001 From: udsamani Date: Wed, 11 Sep 2024 03:07:28 +0100 Subject: [PATCH] s3 storage errors fix --- pkg/storage/error.go | 9 +++++++++ pkg/storage/s3/types.go | 18 ++++++++++-------- 2 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 pkg/storage/error.go diff --git a/pkg/storage/error.go b/pkg/storage/error.go new file mode 100644 index 0000000000..b0cf0227bd --- /dev/null +++ b/pkg/storage/error.go @@ -0,0 +1,9 @@ +package storage + +import "github.com/bacalhau-project/bacalhau/pkg/models" + +const S3_STORAGE_COMPONENT = "S3STOR" + +func NewErrBadS3StorageRequest(msg string) *models.BaseError { + return models.NewBaseError(msg).WithCode(models.NewErrorCode(S3_STORAGE_COMPONENT, 400)) +} diff --git a/pkg/storage/s3/types.go b/pkg/storage/s3/types.go index e09592448f..bd9311bcda 100644 --- a/pkg/storage/s3/types.go +++ b/pkg/storage/s3/types.go @@ -1,12 +1,13 @@ package s3 import ( - "errors" + "fmt" "github.com/fatih/structs" "github.com/mitchellh/mapstructure" "github.com/bacalhau-project/bacalhau/pkg/models" + "github.com/bacalhau-project/bacalhau/pkg/storage" ) type SourceSpec struct { @@ -21,7 +22,7 @@ type SourceSpec struct { func (c SourceSpec) Validate() error { if c.Bucket == "" { - return errors.New("invalid s3 storage params: bucket cannot be empty") + return storage.NewErrBadS3StorageRequest("invalid s3 storage params: bucket cannot be empty") } return nil } @@ -32,11 +33,11 @@ func (c SourceSpec) ToMap() map[string]interface{} { func DecodeSourceSpec(spec *models.SpecConfig) (SourceSpec, error) { if !spec.IsType(models.StorageSourceS3) { - return SourceSpec{}, errors.New("invalid storage source type. expected " + models.StorageSourceS3 + ", but received: " + spec.Type) + return SourceSpec{}, storage.NewErrBadS3StorageRequest("invalid storage source type. expected " + models.StorageSourceS3 + ", but received: " + spec.Type) } inputParams := spec.Params if inputParams == nil { - return SourceSpec{}, errors.New("invalid storage source params. cannot be nil") + return SourceSpec{}, storage.NewErrBadS3StorageRequest("invalid storage source params. cannot be nil") } var c SourceSpec @@ -54,7 +55,7 @@ type PreSignedResultSpec struct { func (c PreSignedResultSpec) Validate() error { if c.PreSignedURL == "" { - return errors.New("invalid s3 signed storage params: signed url cannot be empty") + return storage.NewErrBadS3StorageRequest("invalid s3 signed storage params: signed url cannot be empty") } return c.SourceSpec.Validate() } @@ -65,13 +66,14 @@ func (c PreSignedResultSpec) ToMap() map[string]interface{} { func DecodePreSignedResultSpec(spec *models.SpecConfig) (PreSignedResultSpec, error) { if !spec.IsType(models.StorageSourceS3PreSigned) { - return PreSignedResultSpec{}, errors.New( - "invalid storage source type. expected " + models.StorageSourceS3PreSigned + ", but received: " + spec.Type) + return PreSignedResultSpec{}, storage.NewErrBadS3StorageRequest( + fmt.Sprintf("invalid storage source type. expected %s, but received: %s", + models.StorageSourceS3PreSigned, spec.Type)) } inputParams := spec.Params if inputParams == nil { - return PreSignedResultSpec{}, errors.New("invalid signed result params. cannot be nil") + return PreSignedResultSpec{}, storage.NewErrBadS3StorageRequest("invalid signed result params. cannot be nil") } var c PreSignedResultSpec