From b7305f52201ce7eab2b0b002ba196bb520834793 Mon Sep 17 00:00:00 2001 From: Ryan Johnson Date: Mon, 7 Oct 2024 17:36:03 -0400 Subject: [PATCH] api: tags error handling Checks if `err` is not `nil` after calling `c.Do`. If an error occurs, it returns `nil` and the error. If no error occurs, it returns the `nil` or the result and `nil` depending on the function. This ensures that the functions handle errors gracefully and avoids unspecified behavior during the return process. Ref: #3112 Signed-off-by: Ryan Johnson --- vapi/tags/categories.go | 26 +++++++++++++++++++++----- vapi/tags/tag_association.go | 34 ++++++++++++++++++++++++++-------- vapi/tags/tags.go | 31 ++++++++++++++++++++++++------- 3 files changed, 71 insertions(+), 20 deletions(-) diff --git a/vapi/tags/categories.go b/vapi/tags/categories.go index 64d0b66c1..618ebe481 100644 --- a/vapi/tags/categories.go +++ b/vapi/tags/categories.go @@ -91,7 +91,10 @@ func (c *Manager) CreateCategory(ctx context.Context, category *Category) (strin } url := c.Resource(internal.CategoryPath) var res string - return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil { + return "", err + } + return res, nil } // UpdateCategory updates one or more of the AssociableTypes, Cardinality, @@ -108,13 +111,20 @@ func (c *Manager) UpdateCategory(ctx context.Context, category *Category) error }, } url := c.Resource(internal.CategoryPath).WithID(category.ID) - return c.Do(ctx, url.Request(http.MethodPatch, spec), nil) + req := url.Request(http.MethodPatch, spec) + if err := c.Do(ctx, req, nil); err != nil { + return err + } + return nil } // DeleteCategory deletes a category. func (c *Manager) DeleteCategory(ctx context.Context, category *Category) error { url := c.Resource(internal.CategoryPath).WithID(category.ID) - return c.Do(ctx, url.Request(http.MethodDelete), nil) + if err := c.Do(ctx, url.Request(http.MethodDelete), nil); err != nil { + return err + } + return nil } // GetCategory fetches the category information for the given identifier. @@ -134,14 +144,20 @@ func (c *Manager) GetCategory(ctx context.Context, id string) (*Category, error) } url := c.Resource(internal.CategoryPath).WithID(id) var res Category - return &res, c.Do(ctx, url.Request(http.MethodGet), &res) + if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil { + return nil, err + } + return &res, nil } // ListCategories returns all category IDs in the system. func (c *Manager) ListCategories(ctx context.Context) ([]string, error) { url := c.Resource(internal.CategoryPath) var res []string - return res, c.Do(ctx, url.Request(http.MethodGet), &res) + if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil { + return nil, err + } + return res, nil } // GetCategories fetches a list of category information in the system. diff --git a/vapi/tags/tag_association.go b/vapi/tags/tag_association.go index e844f4e1b..5ba9d09b5 100755 --- a/vapi/tags/tag_association.go +++ b/vapi/tags/tag_association.go @@ -45,7 +45,10 @@ func (c *Manager) AttachTag(ctx context.Context, tagID string, ref mo.Reference) } spec := internal.NewAssociation(ref) url := c.Resource(internal.AssociationPath).WithID(id).WithAction("attach") - return c.Do(ctx, url.Request(http.MethodPost, spec), nil) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), nil); err != nil { + return err + } + return nil } // DetachTag detaches a tag ID from a managed object. @@ -57,7 +60,10 @@ func (c *Manager) DetachTag(ctx context.Context, tagID string, ref mo.Reference) } spec := internal.NewAssociation(ref) url := c.Resource(internal.AssociationPath).WithID(id).WithAction("detach") - return c.Do(ctx, url.Request(http.MethodPost, spec), nil) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), nil); err != nil { + return err + } + return nil } // batchResponse is the response type used by attach/detach operations which @@ -93,7 +99,10 @@ func (c *Manager) AttachTagToMultipleObjects(ctx context.Context, tagID string, }{ids} url := c.Resource(internal.AssociationPath).WithID(id).WithAction("attach-tag-to-multiple-objects") - return c.Do(ctx, url.Request(http.MethodPost, spec), nil) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), nil); err != nil { + return err + } + return nil } // AttachMultipleTagsToObject attaches multiple tag IDs to a managed object. @@ -103,7 +112,7 @@ func (c *Manager) AttachTagToMultipleObjects(ctx context.Context, tagID string, // not successfully attached to the managed object reference it might leave the // managed object reference in a partially tagged state and needs to be resolved // by the caller. In this case BatchErrors is returned and can be used to -// analyse failure reasons on each failed tag. +// analyze failure reasons on each failed tag. // // Specified tagIDs must use URN-notation instead of display names or a generic // error will be returned and no tagging operation will be performed. If the @@ -156,7 +165,7 @@ func (c *Manager) AttachMultipleTagsToObject(ctx context.Context, tagIDs []strin // not successfully detached from the managed object reference it might leave // the managed object reference in a partially tagged state and needs to be // resolved by the caller. In this case BatchErrors is returned and can be used -// to analyse failure reasons on each failed tag. +// to analyze failure reasons on each failed tag. // // Specified tagIDs must use URN-notation instead of display names or a generic // error will be returned and no tagging operation will be performed. If the @@ -207,7 +216,10 @@ func (c *Manager) ListAttachedTags(ctx context.Context, ref mo.Reference) ([]str spec := internal.NewAssociation(ref) url := c.Resource(internal.AssociationPath).WithAction("list-attached-tags") var res []string - return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil { + return nil, err + } + return res, nil } // GetAttachedTags fetches the array of tags attached to the given object. @@ -291,7 +303,10 @@ func (c *Manager) ListAttachedObjectsOnTags(ctx context.Context, tagID []string) url := c.Resource(internal.AssociationPath).WithAction("list-attached-objects-on-tags") var res []AttachedObjects - return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil { + return nil, err + } + return res, nil } // GetAttachedObjectsOnTags combines ListAttachedObjectsOnTags and populates each Tag field. @@ -362,7 +377,10 @@ func (c *Manager) ListAttachedTagsOnObjects(ctx context.Context, objectID []mo.R url := c.Resource(internal.AssociationPath).WithAction("list-attached-tags-on-objects") var res []AttachedTags - return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil { + return nil, err + } + return res, nil } // GetAttachedTagsOnObjects calls ListAttachedTagsOnObjects and populates each Tags field. diff --git a/vapi/tags/tags.go b/vapi/tags/tags.go index b9024f998..655d83195 100644 --- a/vapi/tags/tags.go +++ b/vapi/tags/tags.go @@ -92,7 +92,10 @@ func (c *Manager) CreateTag(ctx context.Context, tag *Tag) (string, error) { } url := c.Resource(internal.TagPath) var res string - return res, c.Do(ctx, url.Request(http.MethodPost, spec), &res) + if err := c.Do(ctx, url.Request(http.MethodPost, spec), &res); err != nil { + return "", err + } + return res, nil } // UpdateTag can update one or both of the tag Description and Name fields. @@ -106,13 +109,19 @@ func (c *Manager) UpdateTag(ctx context.Context, tag *Tag) error { }, } url := c.Resource(internal.TagPath).WithID(tag.ID) - return c.Do(ctx, url.Request(http.MethodPatch, spec), nil) + if err := c.Do(ctx, url.Request(http.MethodPatch, spec), nil); err != nil { + return err + } + return nil } // DeleteTag deletes an existing tag. func (c *Manager) DeleteTag(ctx context.Context, tag *Tag) error { url := c.Resource(internal.TagPath).WithID(tag.ID) - return c.Do(ctx, url.Request(http.MethodDelete), nil) + if err := c.Do(ctx, url.Request(http.MethodDelete), nil); err != nil { + return err + } + return nil } // GetTag fetches the tag information for the given identifier. @@ -133,8 +142,10 @@ func (c *Manager) GetTag(ctx context.Context, id string) (*Tag, error) { url := c.Resource(internal.TagPath).WithID(id) var res Tag - return &res, c.Do(ctx, url.Request(http.MethodGet), &res) - + if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil { + return nil, err + } + return &res, nil } // GetTagForCategory fetches the tag information for the given identifier in the given category. @@ -165,7 +176,10 @@ func (c *Manager) GetTagForCategory(ctx context.Context, id, category string) (* func (c *Manager) ListTags(ctx context.Context) ([]string, error) { url := c.Resource(internal.TagPath) var res []string - return res, c.Do(ctx, url.Request(http.MethodGet), &res) + if err := c.Do(ctx, url.Request(http.MethodGet), &res); err != nil { + return nil, err + } + return res, nil } // GetTags fetches an array of tag information in the system. @@ -203,7 +217,10 @@ func (c *Manager) ListTagsForCategory(ctx context.Context, id string) ([]string, }{id} url := c.Resource(internal.TagPath).WithID(id).WithAction("list-tags-for-category") var res []string - return res, c.Do(ctx, url.Request(http.MethodPost, body), &res) + if err := c.Do(ctx, url.Request(http.MethodPost, body), &res); err != nil { + return nil, err + } + return res, nil } // The id parameter can be a Category ID or Category Name.