Skip to content

Commit 885fe11

Browse files
committed
feat: generalized openapi error constructors, which introduces new core version
1 parent f55dde3 commit 885fe11

File tree

4 files changed

+30
-14
lines changed

4 files changed

+30
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
## Release (2025-XX-YY)
2-
- `alb`: [v0.2.0](services/alb/CHANGELOG.md#v011-2025-03-20)
2+
- `core`: [0.17.0](core/CHANGELOG.md#v0170-2025-03-20)
3+
- **New:** Helper functions for generic openapi error codes
4+
- `alb`: [v0.2.0](services/alb/CHANGELOG.md#v020-2025-03-20)
35
- **New:** API for application load balancer
46
- `cdn`: [v0.1.0](services/cdn/CHANGELOG.md#v010-2025-03-19)
57
- **New:** Introduce new API for content delivery

core/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## v0.17.0 (2025-03-20)
2+
3+
- **New:** Helper functions for generic openapi error codes
4+
15
## v0.16.1 (2025-02-25)
26

37
- **Bugfix:** STACKIT_PRIVATE_KEY and STACKIT_SERVICE_ACCOUNT_KEY can be set via environment variable or via credentials file.

core/oapierror/oapierror.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,23 @@ type GenericOpenAPIError struct {
1919
Model interface{}
2020
}
2121

22+
func NewError(code int, status string) *GenericOpenAPIError {
23+
return &GenericOpenAPIError{
24+
StatusCode: code,
25+
ErrorMessage: status,
26+
Model: map[string]any{},
27+
}
28+
}
29+
30+
func NewErrorWithBody(code int, status string, body []byte, model any) *GenericOpenAPIError {
31+
return &GenericOpenAPIError{
32+
StatusCode: code,
33+
ErrorMessage: status,
34+
Body: body,
35+
Model: model,
36+
}
37+
}
38+
2239
// Error returns non-empty string if there was an errorMessage.
2340
func (e GenericOpenAPIError) Error() string {
2441
// Prevent panic in case of negative value

services/alb/wait/wait_test.go

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,6 @@ func TestCreateOrUpdateLoadbalancerWaitHandler(t *testing.T) {
116116
})
117117
}
118118
}
119-
func httpStatus(code int, status string) *oapierror.GenericOpenAPIError {
120-
return &oapierror.GenericOpenAPIError{
121-
StatusCode: code,
122-
ErrorMessage: status,
123-
Model: map[string]any{},
124-
}
125-
}
126119

127120
func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
128121
tests := []struct {
@@ -133,7 +126,7 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
133126
{
134127
"Delete with '404' succeeded immediately",
135128
[]response{
136-
{nil, httpStatus(http.StatusNotFound, "not found")},
129+
{nil, oapierror.NewError(http.StatusNotFound, "not found")},
137130
},
138131
false,
139132
},
@@ -143,14 +136,14 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
143136
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
144137
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
145138
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
146-
{nil, httpStatus(http.StatusNotFound, "not found")},
139+
{nil, oapierror.NewError(http.StatusNotFound, "not found")},
147140
},
148141
false,
149142
},
150143
{
151144
"Delete with 'gone' succeeded immediately",
152145
[]response{
153-
{nil, httpStatus(http.StatusGone, "gone")},
146+
{nil, oapierror.NewError(http.StatusGone, "gone")},
154147
},
155148
false,
156149
},
@@ -160,7 +153,7 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
160153
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
161154
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
162155
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
163-
{nil, httpStatus(http.StatusGone, "not found")},
156+
{nil, oapierror.NewError(http.StatusGone, "not found")},
164157
},
165158
false,
166159
},
@@ -170,7 +163,7 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
170163
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
171164
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
172165
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
173-
{&alb.LoadBalancer{Status: utils.Ptr(string(StatusError))}, httpStatus(http.StatusInternalServerError, "kapow")},
166+
{&alb.LoadBalancer{Status: utils.Ptr(string(StatusError))}, oapierror.NewError(http.StatusInternalServerError, "kapow")},
174167
},
175168
true,
176169
},
@@ -180,7 +173,7 @@ func TestDeleteLoadbalancerWaitHandler(t *testing.T) {
180173
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
181174
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
182175
{&alb.LoadBalancer{Status: utils.Ptr(StatusPending)}, nil},
183-
{&alb.LoadBalancer{Status: utils.Ptr(string(StatusError))}, httpStatus(http.StatusOK, "ok")},
176+
{&alb.LoadBalancer{Status: utils.Ptr(string(StatusError))}, oapierror.NewError(http.StatusOK, "ok")},
184177
},
185178
true,
186179
},

0 commit comments

Comments
 (0)