Skip to content

Commit

Permalink
refactor: check success by 2xx range
Browse files Browse the repository at this point in the history
  • Loading branch information
uleMeshcloud committed Dec 10, 2024
1 parent 2d35645 commit ee88870
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 33 deletions.
8 changes: 2 additions & 6 deletions client/buildingblock.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*MeshBuildingB
return nil, nil
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var bb MeshBuildingBlock
err = json.Unmarshal(data, &bb)
Expand Down Expand Up @@ -141,9 +139,7 @@ func (c *MeshStackProviderClient) CreateBuildingBlock(bb *MeshBuildingBlockCreat
return nil, err
}

if res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdBb MeshBuildingBlock
err = json.Unmarshal(data, &createdBb)
Expand Down
8 changes: 2 additions & 6 deletions client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ func (c *MeshStackProviderClient) CreateProject(project *MeshProjectCreate) (*Me
return nil, err
}

if res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdProject MeshProject
err = json.Unmarshal(data, &createdProject)
Expand Down Expand Up @@ -219,9 +217,7 @@ func (c *MeshStackProviderClient) UpdateProject(project *MeshProjectCreate) (*Me
return nil, err
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var updatedProject MeshProject
err = json.Unmarshal(data, &updatedProject)
Expand Down
8 changes: 2 additions & 6 deletions client/project_binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,7 @@ func (c *MeshStackProviderClient) readProjectBinding(name string, contentType st
return nil, nil
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var binding MeshProjectBinding
err = json.Unmarshal(data, &binding)
Expand Down Expand Up @@ -120,9 +118,7 @@ func (c *MeshStackProviderClient) createProjectBinding(binding *MeshProjectBindi
return nil, err
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdBinding MeshProjectBinding
err = json.Unmarshal(data, &createdBinding)
Expand Down
20 changes: 20 additions & 0 deletions client/status_code_checker.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package client

import (
"io"
"fmt"
"net/http"
)

func isSuccessHTTPStatusOrThrow(resp *http.Response) error {
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err

Check failure on line 12 in client/status_code_checker.go

View workflow job for this annotation

GitHub Actions / Build

too many return values

Check failure on line 12 in client/status_code_checker.go

View workflow job for this annotation

GitHub Actions / generate

too many return values

Check failure on line 12 in client/status_code_checker.go

View workflow job for this annotation

GitHub Actions / Build

too many return values

Check failure on line 12 in client/status_code_checker.go

View workflow job for this annotation

GitHub Actions / generate

too many return values
}

if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("unexpected status code: got %d, expected 2xx range\nBody: %s", resp.StatusCode, data)
}

return nil
}
12 changes: 3 additions & 9 deletions client/tag_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,7 @@ func (c *MeshStackProviderClient) ReadTagDefinition(name string) (*MeshTagDefini
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to read tag definition: %s", resp.Status)
}
isSuccessHTTPStatusOrThrow(resp)

var tagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&tagDefinition); err != nil {
Expand Down Expand Up @@ -191,9 +189,7 @@ func (c *MeshStackProviderClient) CreateTagDefinition(tagDefinition *MeshTagDefi
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("failed to create tag definition: %s", resp.Status)
}
isSuccessHTTPStatusOrThrow(resp)

var createdTagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&createdTagDefinition); err != nil {
Expand Down Expand Up @@ -224,9 +220,7 @@ func (c *MeshStackProviderClient) UpdateTagDefinition(tagDefinition *MeshTagDefi
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to update tag definition: %s", resp.Status)
}
isSuccessHTTPStatusOrThrow(resp)

var updatedTagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&updatedTagDefinition); err != nil {
Expand Down
8 changes: 2 additions & 6 deletions client/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ func (c *MeshStackProviderClient) ReadTenant(workspace string, project string, p
return nil, nil
}

if res.StatusCode != 200 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var tenant MeshTenant
err = json.Unmarshal(data, &tenant)
Expand Down Expand Up @@ -121,9 +119,7 @@ func (c *MeshStackProviderClient) CreateTenant(tenant *MeshTenantCreate) (*MeshT
return nil, err
}

if res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}
isSuccessHTTPStatusOrThrow(res)

var createdTenant MeshTenant
err = json.Unmarshal(data, &createdTenant)
Expand Down

0 comments on commit ee88870

Please sign in to comment.