Skip to content

Commit

Permalink
fix: return error of the API endpoints & SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Dec 7, 2023
1 parent ab660e2 commit 7eefb22
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 26 deletions.
20 changes: 4 additions & 16 deletions api/v1/net_services_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import (
)

func netServiceStart(resp http.ResponseWriter, ns *netRestrictService, ifaceName string) error {
if ns == nil || ns.service == nil {
sendJSONError(resp, MetaMessage{
Type: APIMetaMessageTypeError,
Slug: SlugServiceNotInitialized,
Title: "Service not initiated",
Message: "To get the status of the service, it must be started first.",
}, http.StatusOK)
if !ensureServiceInitialized(resp, ns) {
return ErrServiceNotInitialized
}

Expand Down Expand Up @@ -43,13 +37,7 @@ func netServiceStart(resp http.ResponseWriter, ns *netRestrictService, ifaceName
}

func netServiceStop(resp http.ResponseWriter, ns *netRestrictService) error {
if ns == nil || ns.service == nil {
sendJSONError(resp, MetaMessage{
Type: APIMetaMessageTypeError,
Slug: SlugServiceNotInitialized,
Title: "Service not initiated",
Message: "To get the status of the service, it must be started first.",
}, http.StatusOK)
if !ensureServiceInitialized(resp, ns) {
return ErrServiceNotInitialized
}

Expand Down Expand Up @@ -104,7 +92,7 @@ func netServiceStatus(resp http.ResponseWriter, ns *netRestrictService) error {
Slug: SlugServiceNotInitialized,
Title: "Service not initiated",
Message: "To get the status of the service, it must be started first.",
}, http.StatusOK)
}, http.StatusInternalServerError)
return ErrServiceNotInitialized
}

Expand All @@ -125,7 +113,7 @@ func netServiceStatus(resp http.ResponseWriter, ns *netRestrictService) error {
}

func ensureServiceInitialized(resp http.ResponseWriter, ns *netRestrictService) bool {
if ns != nil {
if ns != nil && ns.service != nil {
return true
}
sendJSONError(resp,
Expand Down
20 changes: 10 additions & 10 deletions sdk/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ func (c *Client) getServiceStatus(resPath string) (*api.MetaMessage, error) {
}

func (c *Client) postServiceAction(resPath string, req interface{}) error {
resp, resErr := c.postResource(resPath, req)
// Since the response body can have more information about the error, we try to parse it
if resErr != nil && resp == nil {
return fmt.Errorf("postResource: %w", resErr)
resp, err := c.postResource(resPath, req)
if err == nil {
return nil
}

msg := api.MetaMessage{}
if err := json.Unmarshal(resp, &msg); err == nil {
return Error{Message: msg}
if len(resp) == 0 {
return fmt.Errorf("postResource: %w", err)
}

// if the response is not a MetaMessage, it's probably a success message
// Therefore we just return the original error from the request which is nil on success
return resErr
msg := api.MetaMessage{}
if err := json.Unmarshal(resp, &msg); err != nil {
return fmt.Errorf("raw output: %s", string(resp))
}
return Error{Message: msg}
}

0 comments on commit 7eefb22

Please sign in to comment.