Skip to content
This repository has been archived by the owner on Jan 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #53 from owncloud/error-handling-codes
Browse files Browse the repository at this point in the history
Adopt go-micro error codes and id
  • Loading branch information
kulmann authored Aug 20, 2020
2 parents 5249eec + 87db549 commit e74e9b0
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 55 deletions.
16 changes: 8 additions & 8 deletions pkg/proto/v0/settings.pb.micro_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,39 +103,39 @@ func TestSettingsBundleProperties(t *testing.T) {
"सिम्प्ले-display-name",
"सिम्प्ले-extension-name",
"सिम्प्ले",
merrors.New("", "extension: must be in a valid format; name: must be in a valid format.", 0),
merrors.New("ocis-settings", "extension: must be in a valid format; name: must be in a valid format.", 400),
},
{
"UTF validation on display name",
"सिम्प्ले-bundle-name",
"सिम्प्ले-display-name",
"simple-extension-name",
"123e4567-e89b-12d3-a456-426652340000",
merrors.New("", "name: must be in a valid format.", 0),
merrors.New("ocis-settings", "name: must be in a valid format.", 400),
},
{
"extension name with ../ in the name",
"bundle-name",
"simple-display-name",
"../folder-a-level-higher-up",
"123e4567-e89b-12d3-a456-426652340000",
merrors.New("", "extension: must be in a valid format.", 0),
merrors.New("ocis-settings", "extension: must be in a valid format.", 400),
},
{
"extension name with \\ in the name",
"bundle-name",
"simple-display-name",
"\\",
"123e4567-e89b-12d3-a456-426652340000",
merrors.New("", "extension: must be in a valid format.", 0),
merrors.New("ocis-settings", "extension: must be in a valid format.", 400),
},
{
"spaces are disallowed in bundle names",
"bundle name",
"simple display name",
"simple extension name",
"123e4567-e89b-12d3-a456-426652340000",
merrors.New("", "extension: must be in a valid format; name: must be in a valid format.", 0),
merrors.New("ocis-settings", "extension: must be in a valid format; name: must be in a valid format.", 400),
},
{
"spaces are allowed in display names",
Expand All @@ -151,15 +151,15 @@ func TestSettingsBundleProperties(t *testing.T) {
"simple-display-name",
"",
"123e4567-e89b-12d3-a456-426652340000",
merrors.New("", "extension: cannot be blank.", 0),
merrors.New("ocis-settings", "extension: cannot be blank.", 400),
},
{
"display name missing",
"bundleName",
"",
"simple-extension-name",
"123e4567-e89b-12d3-a456-426652340000",
merrors.New("", "display_name: cannot be blank.", 0),
merrors.New("ocis-settings", "display_name: cannot be blank.", 400),
},
{
"UUID missing (omitted on bundles)",
Expand Down Expand Up @@ -216,7 +216,7 @@ func TestSettingsBundleWithoutSettings(t *testing.T) {
response, err := cl.SaveBundle(context.Background(), &createRequest)
assert.Error(t, err)
assert.Nil(t, response)
assert.Equal(t, merrors.New("", "extension: cannot be blank; name: cannot be blank; settings: cannot be blank.", 0), err)
assert.Equal(t, merrors.New("ocis-settings", "extension: cannot be blank; name: cannot be blank; settings: cannot be blank.", 400), err)
os.RemoveAll(dataStore)
}

Expand Down
101 changes: 54 additions & 47 deletions pkg/service/v0/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,11 @@ func NewService(cfg *config.Config, logger log.Logger) Service {
func (g Service) SaveBundle(c context.Context, req *proto.SaveBundleRequest, res *proto.SaveBundleResponse) error {
cleanUpResource(c, req.Bundle.Resource)
if validationError := validateSaveBundle(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.WriteBundle(req.Bundle)
if err != nil {
return merrors.FromError(err)
return merrors.BadRequest("ocis-settings", "%s", err)
}
res.Bundle = r
return nil
Expand All @@ -65,11 +65,11 @@ func (g Service) SaveBundle(c context.Context, req *proto.SaveBundleRequest, res
// GetBundle implements the BundleServiceHandler interface
func (g Service) GetBundle(c context.Context, req *proto.GetBundleRequest, res *proto.GetBundleResponse) error {
if validationError := validateGetBundle(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.ReadBundle(req.BundleId)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
res.Bundle = r
return nil
Expand All @@ -80,11 +80,11 @@ func (g Service) ListBundles(c context.Context, req *proto.ListBundlesRequest, r
// fetch all bundles
req.AccountUuid = getValidatedAccountUUID(c, req.AccountUuid)
if validationError := validateListBundles(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
bundles, err := g.manager.ListBundles(proto.Bundle_TYPE_DEFAULT)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
res.Bundles = bundles
return nil
Expand All @@ -94,11 +94,11 @@ func (g Service) ListBundles(c context.Context, req *proto.ListBundlesRequest, r
func (g Service) AddSettingToBundle(c context.Context, req *proto.AddSettingToBundleRequest, res *proto.AddSettingToBundleResponse) error {
cleanUpResource(c, req.Setting.Resource)
if validationError := validateAddSettingToBundle(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.AddSettingToBundle(req.BundleId, req.Setting)
if err != nil {
return merrors.FromError(err)
return merrors.BadRequest("ocis-settings", "%s", err)
}
res.Setting = r
return nil
Expand All @@ -107,9 +107,13 @@ func (g Service) AddSettingToBundle(c context.Context, req *proto.AddSettingToBu
// RemoveSettingFromBundle implements the BundleServiceHandler interface
func (g Service) RemoveSettingFromBundle(c context.Context, req *proto.RemoveSettingFromBundleRequest, _ *empty.Empty) error {
if validationError := validateRemoveSettingFromBundle(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
return g.manager.RemoveSettingFromBundle(req.BundleId, req.SettingId)
if err := g.manager.RemoveSettingFromBundle(req.BundleId, req.SettingId); err != nil {
return merrors.BadRequest("ocis-settings", "%s", err)
}

return nil
}

// SaveValue implements the ValueServiceHandler interface
Expand All @@ -118,15 +122,15 @@ func (g Service) SaveValue(c context.Context, req *proto.SaveValueRequest, res *
cleanUpResource(c, req.Value.Resource)
// TODO: we need to check, if the authenticated user has permission to write the value for the specified resource (e.g. global, file with id xy, ...)
if validationError := validateSaveValue(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.WriteValue(req.Value)
if err != nil {
return merrors.FromError(err)
return merrors.BadRequest("ocis-settings", "%s", err)
}
valueWithIdentifier, err := g.getValueWithIdentifier(r)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
res.Value = valueWithIdentifier
return nil
Expand All @@ -135,15 +139,15 @@ func (g Service) SaveValue(c context.Context, req *proto.SaveValueRequest, res *
// GetValue implements the ValueServiceHandler interface
func (g Service) GetValue(c context.Context, req *proto.GetValueRequest, res *proto.GetValueResponse) error {
if validationError := validateGetValue(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.ReadValue(req.Id)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
valueWithIdentifier, err := g.getValueWithIdentifier(r)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
res.Value = valueWithIdentifier
return nil
Expand All @@ -153,13 +157,13 @@ func (g Service) GetValue(c context.Context, req *proto.GetValueRequest, res *pr
func (g Service) GetValueByUniqueIdentifiers(ctx context.Context, in *proto.GetValueByUniqueIdentifiersRequest, res *proto.GetValueResponse) error {
v, err := g.manager.ReadValueByUniqueIdentifiers(in.AccountUuid, in.SettingId)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}

if v.BundleId != "" {
valueWithIdentifier, err := g.getValueWithIdentifier(v)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}

res.Value = valueWithIdentifier
Expand All @@ -171,11 +175,11 @@ func (g Service) GetValueByUniqueIdentifiers(ctx context.Context, in *proto.GetV
func (g Service) ListValues(c context.Context, req *proto.ListValuesRequest, res *proto.ListValuesResponse) error {
req.AccountUuid = getValidatedAccountUUID(c, req.AccountUuid)
if validationError := validateListValues(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.ListValues(req.BundleId, req.AccountUuid)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
var result []*proto.ValueWithIdentifier
for _, value := range r {
Expand All @@ -188,34 +192,15 @@ func (g Service) ListValues(c context.Context, req *proto.ListValuesRequest, res
return nil
}

func (g Service) getValueWithIdentifier(value *proto.Value) (*proto.ValueWithIdentifier, error) {
bundle, err := g.manager.ReadBundle(value.BundleId)
if err != nil {
return nil, err
}
setting, err := g.manager.ReadSetting(value.SettingId)
if err != nil {
return nil, err
}
return &proto.ValueWithIdentifier{
Identifier: &proto.Identifier{
Extension: bundle.Extension,
Bundle: bundle.Name,
Setting: setting.Name,
},
Value: value,
}, nil
}

// ListRoles implements the RoleServiceHandler interface
func (g Service) ListRoles(c context.Context, req *proto.ListBundlesRequest, res *proto.ListBundlesResponse) error {
req.AccountUuid = getValidatedAccountUUID(c, req.AccountUuid)
if validationError := validateListRoles(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.ListBundles(proto.Bundle_TYPE_ROLE)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
res.Bundles = r
return nil
Expand All @@ -225,11 +210,11 @@ func (g Service) ListRoles(c context.Context, req *proto.ListBundlesRequest, res
func (g Service) ListRoleAssignments(c context.Context, req *proto.ListRoleAssignmentsRequest, res *proto.ListRoleAssignmentsResponse) error {
req.AccountUuid = getValidatedAccountUUID(c, req.AccountUuid)
if validationError := validateListRoleAssignments(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.ListRoleAssignments(req.AccountUuid)
if err != nil {
return merrors.FromError(err)
return merrors.NotFound("ocis-settings", "%s", err)
}
res.Assignments = r
return nil
Expand All @@ -239,11 +224,11 @@ func (g Service) ListRoleAssignments(c context.Context, req *proto.ListRoleAssig
func (g Service) AssignRoleToUser(c context.Context, req *proto.AssignRoleToUserRequest, res *proto.AssignRoleToUserResponse) error {
req.AccountUuid = getValidatedAccountUUID(c, req.AccountUuid)
if validationError := validateAssignRoleToUser(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
r, err := g.manager.WriteRoleAssignment(req.AccountUuid, req.RoleId)
if err != nil {
return merrors.FromError(err)
return merrors.BadRequest("ocis-settings", "%s", err)
}
res.Assignment = r
return nil
Expand All @@ -252,9 +237,12 @@ func (g Service) AssignRoleToUser(c context.Context, req *proto.AssignRoleToUser
// RemoveRoleFromUser implements the RoleServiceHandler interface
func (g Service) RemoveRoleFromUser(c context.Context, req *proto.RemoveRoleFromUserRequest, _ *empty.Empty) error {
if validationError := validateRemoveRoleFromUser(req); validationError != nil {
return merrors.FromError(validationError)
return merrors.BadRequest("ocis-settings", "%s", validationError)
}
return g.manager.RemoveRoleAssignment(req.Id)
if err := g.manager.RemoveRoleAssignment(req.Id); err != nil {
return merrors.BadRequest("ocis-settings", "%s", err)
}
return nil
}

// cleanUpResource makes sure that the account uuid of the authenticated user is injected if needed.
Expand All @@ -274,3 +262,22 @@ func getValidatedAccountUUID(c context.Context, accountUUID string) string {
}
return accountUUID
}

func (g Service) getValueWithIdentifier(value *proto.Value) (*proto.ValueWithIdentifier, error) {
bundle, err := g.manager.ReadBundle(value.BundleId)
if err != nil {
return nil, err
}
setting, err := g.manager.ReadSetting(value.SettingId)
if err != nil {
return nil, err
}
return &proto.ValueWithIdentifier{
Identifier: &proto.Identifier{
Extension: bundle.Extension,
Bundle: bundle.Name,
Setting: setting.Name,
},
Value: value,
}, nil
}

0 comments on commit e74e9b0

Please sign in to comment.