Skip to content

Commit 2eb199e

Browse files
committed
refactor: check success by 2xx range
1 parent 2d35645 commit 2eb199e

File tree

7 files changed

+31
-14
lines changed

7 files changed

+31
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.6.1
2+
REFACTOR:
3+
- Validate success responses by checking for HTTP status codes in the 2xx range
4+
15
## v0.6.0
26

37
FEATURES:

client/buildingblock.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*MeshBuildingB
103103
return nil, nil
104104
}
105105

106-
if res.StatusCode != 200 {
106+
if !isSuccessHTTPStatus(res) {
107107
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
108108
}
109109

@@ -141,7 +141,7 @@ func (c *MeshStackProviderClient) CreateBuildingBlock(bb *MeshBuildingBlockCreat
141141
return nil, err
142142
}
143143

144-
if res.StatusCode != 201 {
144+
if !isSuccessHTTPStatus(res) {
145145
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
146146
}
147147

client/project.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (c *MeshStackProviderClient) ReadProject(workspace string, name string) (*M
7171
return nil, nil
7272
}
7373

74-
if res.StatusCode != 200 {
74+
if !isSuccessHTTPStatus(res) {
7575
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
7676
}
7777

@@ -119,7 +119,7 @@ func (c *MeshStackProviderClient) ReadProjects(workspaceIdentifier string, payme
119119
return nil, fmt.Errorf("failed to read response body: %w", err)
120120
}
121121

122-
if res.StatusCode != http.StatusOK {
122+
if !isSuccessHTTPStatus(res) {
123123
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
124124
}
125125

@@ -178,7 +178,7 @@ func (c *MeshStackProviderClient) CreateProject(project *MeshProjectCreate) (*Me
178178
return nil, err
179179
}
180180

181-
if res.StatusCode != 201 {
181+
if !isSuccessHTTPStatus(res) {
182182
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
183183
}
184184

@@ -219,7 +219,7 @@ func (c *MeshStackProviderClient) UpdateProject(project *MeshProjectCreate) (*Me
219219
return nil, err
220220
}
221221

222-
if res.StatusCode != 200 {
222+
if !isSuccessHTTPStatus(res) {
223223
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
224224
}
225225

client/project_binding.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ func (c *MeshStackProviderClient) readProjectBinding(name string, contentType st
7070
return nil, nil
7171
}
7272

73-
if res.StatusCode != 200 {
73+
if !isSuccessHTTPStatus(res) {
7474
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
7575
}
7676

@@ -120,7 +120,7 @@ func (c *MeshStackProviderClient) createProjectBinding(binding *MeshProjectBindi
120120
return nil, err
121121
}
122122

123-
if res.StatusCode != 200 {
123+
if !isSuccessHTTPStatus(res) {
124124
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
125125
}
126126

client/status_code_checker.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package client
2+
3+
import (
4+
"net/http"
5+
)
6+
7+
func isSuccessHTTPStatus(resp *http.Response) bool {
8+
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
9+
return false
10+
}
11+
12+
return true
13+
}

client/tag_definition.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ func (c *MeshStackProviderClient) ReadTagDefinitions() (*[]MeshTagDefinition, er
107107
return nil, fmt.Errorf("failed to read response body: %w", err)
108108
}
109109

110-
if res.StatusCode != http.StatusOK {
110+
if !isSuccessHTTPStatus(res) {
111111
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
112112
}
113113

@@ -156,7 +156,7 @@ func (c *MeshStackProviderClient) ReadTagDefinition(name string) (*MeshTagDefini
156156
}
157157
defer resp.Body.Close()
158158

159-
if resp.StatusCode != http.StatusOK {
159+
if !isSuccessHTTPStatus(resp) {
160160
return nil, fmt.Errorf("failed to read tag definition: %s", resp.Status)
161161
}
162162

@@ -191,7 +191,7 @@ func (c *MeshStackProviderClient) CreateTagDefinition(tagDefinition *MeshTagDefi
191191
}
192192
defer resp.Body.Close()
193193

194-
if resp.StatusCode != http.StatusCreated {
194+
if !isSuccessHTTPStatus(resp) {
195195
return nil, fmt.Errorf("failed to create tag definition: %s", resp.Status)
196196
}
197197

@@ -224,7 +224,7 @@ func (c *MeshStackProviderClient) UpdateTagDefinition(tagDefinition *MeshTagDefi
224224
}
225225
defer resp.Body.Close()
226226

227-
if resp.StatusCode != http.StatusOK {
227+
if !isSuccessHTTPStatus(resp) {
228228
return nil, fmt.Errorf("failed to update tag definition: %s", resp.Status)
229229
}
230230

client/tenant.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func (c *MeshStackProviderClient) ReadTenant(workspace string, project string, p
8383
return nil, nil
8484
}
8585

86-
if res.StatusCode != 200 {
86+
if !isSuccessHTTPStatus(res) {
8787
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
8888
}
8989

@@ -121,7 +121,7 @@ func (c *MeshStackProviderClient) CreateTenant(tenant *MeshTenantCreate) (*MeshT
121121
return nil, err
122122
}
123123

124-
if res.StatusCode != 201 {
124+
if !isSuccessHTTPStatus(res) {
125125
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
126126
}
127127

0 commit comments

Comments
 (0)