Skip to content

Commit ef2cf50

Browse files
500 custom prop type (kubeflow#914)
* improve error handling package Signed-off-by: Adysen Rothman <[email protected]> * add testing Signed-off-by: Adysen Rothman <[email protected]> * more specific err for mlmd Signed-off-by: Adysen Rothman <[email protected]> * api err for type val issues Signed-off-by: Adysen Rothman <[email protected]> --------- Signed-off-by: Adysen Rothman <[email protected]>
1 parent 64ef62f commit ef2cf50

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

internal/converter/mlmd_openapi_converter_util.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99

1010
"github.com/kubeflow/model-registry/internal/defaults"
1111
"github.com/kubeflow/model-registry/internal/ml_metadata/proto"
12+
"github.com/kubeflow/model-registry/pkg/api"
1213
"github.com/kubeflow/model-registry/pkg/openapi"
1314
)
1415

@@ -76,7 +77,7 @@ func MapMLMDCustomProperties(source map[string]*proto.Value) (map[string]openapi
7677
b64 := base64.StdEncoding.EncodeToString(asJSON)
7778
customValue.MetadataStructValue = NewMetadataStructValue(b64)
7879
default:
79-
return nil, fmt.Errorf("type mapping not found for %s:%v", key, v)
80+
return nil, fmt.Errorf("%w: metadataType not found for %s: %v", api.ErrBadRequest, key, v)
8081
}
8182

8283
data[key] = customValue

internal/converter/openapi_mlmd_converter_util.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/google/uuid"
1010
"github.com/kubeflow/model-registry/internal/defaults"
1111
"github.com/kubeflow/model-registry/internal/ml_metadata/proto"
12+
"github.com/kubeflow/model-registry/pkg/api"
1213
"github.com/kubeflow/model-registry/pkg/openapi"
1314
"google.golang.org/protobuf/types/known/structpb"
1415
)
@@ -65,7 +66,7 @@ func MapOpenAPICustomProperties(source *map[string]openapi.MetadataValue) (map[s
6566
case v.MetadataIntValue != nil:
6667
intValue, err := StringToInt64(&v.MetadataIntValue.IntValue)
6768
if err != nil {
68-
return nil, fmt.Errorf("unable to decode as int64 %w for key %s", err, key)
69+
return nil, fmt.Errorf("%w: unable to decode as int64 %w for key %s", api.ErrBadRequest, err, key)
6970
}
7071
value.Value = &proto.Value_IntValue{IntValue: *intValue}
7172
// double value
@@ -78,22 +79,22 @@ func MapOpenAPICustomProperties(source *map[string]openapi.MetadataValue) (map[s
7879
case v.MetadataStructValue != nil:
7980
data, err := base64.StdEncoding.DecodeString(v.MetadataStructValue.StructValue)
8081
if err != nil {
81-
return nil, fmt.Errorf("unable to decode %w for key %s", err, key)
82+
return nil, fmt.Errorf("%w: unable to decode %w for key %s", api.ErrBadRequest, err, key)
8283
}
8384
var asMap map[string]interface{}
8485
err = json.Unmarshal(data, &asMap)
8586
if err != nil {
86-
return nil, fmt.Errorf("unable to decode %w for key %s", err, key)
87+
return nil, fmt.Errorf("%w: unable to decode %w for key %s", api.ErrBadRequest, err, key)
8788
}
8889
asStruct, err := structpb.NewStruct(asMap)
8990
if err != nil {
90-
return nil, fmt.Errorf("unable to decode %w for key %s", err, key)
91+
return nil, fmt.Errorf("%w: unable to decode %w for key %s", api.ErrBadRequest, err, key)
9192
}
9293
value.Value = &proto.Value_StructValue{
9394
StructValue: asStruct,
9495
}
9596
default:
96-
return nil, fmt.Errorf("type mapping not found for %s:%v", key, v)
97+
return nil, fmt.Errorf("%w: metadataType not found for %s: %v", api.ErrBadRequest, key, v)
9798
}
9899

99100
props[key] = &value

pkg/api/error.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ func ErrToStatus(err error) int {
2626
}
2727
}
2828

29-
switch errors.Unwrap(err) {
30-
case ErrBadRequest:
29+
if errors.Is(err, ErrBadRequest) {
3130
return http.StatusBadRequest
32-
case ErrNotFound:
31+
}
32+
if errors.Is(err, ErrNotFound) {
3333
return http.StatusNotFound
34-
default:
35-
return http.StatusInternalServerError
3634
}
35+
36+
// Default error to return
37+
return http.StatusInternalServerError
3738
}

pkg/core/registered_model_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -464,3 +464,24 @@ func (suite *CoreTestSuite) TestGetRegisteredModelsWithPageSize() {
464464
suite.Equal(*secondModel.Id, *truncatedList.Items[0].Id)
465465
suite.Equal(*thirdModel.Id, *truncatedList.Items[1].Id)
466466
}
467+
468+
func (suite *CoreTestSuite) TestCreateRegisteredModelWithCustomPropFailure() {
469+
// create mode registry service
470+
service := suite.setupModelRegistryService()
471+
472+
// register a new model with incomplete customProperty fields
473+
registeredModel := &openapi.RegisteredModel{
474+
Name: modelName,
475+
CustomProperties: &map[string]openapi.MetadataValue{
476+
"myCustomProp1": {},
477+
},
478+
}
479+
480+
// test
481+
_, err := service.UpsertRegisteredModel(registeredModel)
482+
483+
// checks
484+
statusResp := api.ErrToStatus(err)
485+
suite.NotNilf(err, "error creating registered model: %v", err)
486+
suite.Equal(400, statusResp, "customProperties must include metadataType")
487+
}

0 commit comments

Comments
 (0)