Skip to content

Commit b11335d

Browse files
committed
FEATURE/MINOR: runtime-maps: improved handling http status codes
1 parent 78b4820 commit b11335d

File tree

4 files changed

+54
-37
lines changed

4 files changed

+54
-37
lines changed

go.mod

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require (
1515
github.com/go-openapi/strfmt v0.19.5
1616
github.com/go-openapi/swag v0.19.9
1717
github.com/go-openapi/validate v0.19.8
18-
github.com/haproxytech/client-native/v2 v2.0.1-0.20200507114430-4866106427e6
18+
github.com/haproxytech/client-native/v2 v2.0.1-0.20200507134651-011eb67629f8
1919
github.com/haproxytech/config-parser/v2 v2.0.1
2020
github.com/haproxytech/models/v2 v2.0.1-0.20200507113249-2280458b45e8
2121
github.com/jessevdk/go-flags v1.4.0
@@ -26,4 +26,3 @@ require (
2626
golang.org/x/sys v0.0.0-20200420163511-1957bb5e6d1f
2727
gopkg.in/yaml.v2 v2.2.8
2828
)
29-

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,8 @@ github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
127127
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
128128
github.com/haproxytech/client-native/v2 v2.0.0 h1:kWwfsM7u/L4mJ3AQW+9E0wzcwoY6eA1bq2KJWYlS7Dk=
129129
github.com/haproxytech/client-native/v2 v2.0.0/go.mod h1:+cVzv7MaoF68Jz5CuXmdjnui9a1mGCwl1VcCVgXZ9Js=
130-
github.com/haproxytech/client-native/v2 v2.0.1-0.20200507114430-4866106427e6 h1:iWGR/zTRIDprBrc+fHghKecBkSSB6lLL5IyjcpKYBzE=
131-
github.com/haproxytech/client-native/v2 v2.0.1-0.20200507114430-4866106427e6/go.mod h1:9ieyq45piVEwi2Fpknvw7PpciyAbgtESIXBffK8VUwo=
130+
github.com/haproxytech/client-native/v2 v2.0.1-0.20200507134651-011eb67629f8 h1:xyWQg2REHiIHwfAZwCcvHG4KRppm+vvRNJrYjLtTou0=
131+
github.com/haproxytech/client-native/v2 v2.0.1-0.20200507134651-011eb67629f8/go.mod h1:9ieyq45piVEwi2Fpknvw7PpciyAbgtESIXBffK8VUwo=
132132
github.com/haproxytech/config-parser/v2 v2.0.1 h1:737eWpu+qKCiEx8caeXdvIq599qKCBd18Lc9+zyzgx0=
133133
github.com/haproxytech/config-parser/v2 v2.0.1/go.mod h1:VjHOetXAJtwrtLAJPoDaD/kEYwCoixVp5SCsBitMcj8=
134134
github.com/haproxytech/models/v2 v2.0.0 h1:okLkj9Ht5D2UnDe46huyf2TfP/mAdhTE8cB3u91nWhY=

handlers/map.go

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,10 @@
1616
package handlers
1717

1818
import (
19-
"fmt"
20-
2119
"github.com/go-openapi/runtime/middleware"
2220
client_native "github.com/haproxytech/client-native/v2"
2321
"github.com/haproxytech/dataplaneapi/misc"
2422
"github.com/haproxytech/dataplaneapi/operations/maps"
25-
"github.com/haproxytech/models/v2"
2623
)
2724

2825
//MapsCreateRuntimeMapHandlerImpl implementation of the MapsCreateRuntimeMapHandler interface using client-native client
@@ -39,8 +36,8 @@ func (h *MapsCreateRuntimeMapHandlerImpl) Handle(params maps.CreateRuntimeMapPar
3936

4037
me, err := h.Client.Runtime.CreateMap(file, *header)
4138
if err != nil {
42-
e := misc.HandleError(err)
43-
return maps.NewCreateRuntimeMapDefault(int(*e.Code)).WithPayload(e)
39+
status := misc.GetHTTPStatusFromErr(err)
40+
return maps.NewCreateRuntimeMapDefault(status).WithPayload(misc.SetError(status, err.Error()))
4441
}
4542
return maps.NewCreateRuntimeMapCreated().WithPayload(me)
4643
}
@@ -54,8 +51,8 @@ type GetMapsHandlerImpl struct {
5451
func (h *GetMapsHandlerImpl) Handle(params maps.GetAllRuntimeMapFilesParams, principal interface{}) middleware.Responder {
5552
mapFiles, err := h.Client.Runtime.ShowMaps()
5653
if err != nil {
57-
e := misc.HandleError(err)
58-
return maps.NewShowRuntimeMapDefault(int(*e.Code)).WithPayload(e)
54+
status := misc.GetHTTPStatusFromErr(err)
55+
return maps.NewShowRuntimeMapDefault(status).WithPayload(misc.SetError(status, err.Error()))
5956
}
6057
return maps.NewGetAllRuntimeMapFilesOK().WithPayload(mapFiles)
6158
}
@@ -68,8 +65,8 @@ type GetMapHandlerImpl struct {
6865
func (h *GetMapHandlerImpl) Handle(params maps.GetOneRuntimeMapParams, principal interface{}) middleware.Responder {
6966
m, err := h.Client.Runtime.GetMap(params.Name)
7067
if err != nil {
71-
e := misc.HandleError(err)
72-
return maps.NewGetOneRuntimeMapDefault(int(*e.Code)).WithPayload(e)
68+
status := misc.GetHTTPStatusFromErr(err)
69+
return maps.NewGetOneRuntimeMapDefault(status).WithPayload(misc.SetError(status, err.Error()))
7370
}
7471
if m == nil {
7572
return maps.NewGetOneRuntimeMapNotFound()
@@ -89,8 +86,8 @@ func (h *ClearMapHandlerImpl) Handle(params maps.ClearRuntimeMapParams, principa
8986
}
9087
err := h.Client.Runtime.ClearMap(params.Name, forceDelete)
9188
if err != nil {
92-
e := misc.HandleError(err)
93-
return maps.NewClearRuntimeMapDefault(int(*e.Code)).WithPayload(e)
89+
status := misc.GetHTTPStatusFromErr(err)
90+
return maps.NewClearRuntimeMapDefault(status).WithPayload(misc.SetError(status, err.Error()))
9491
}
9592
return maps.NewClearRuntimeMapNoContent()
9693
}
@@ -103,8 +100,8 @@ type ShowMapHandlerImpl struct {
103100
func (h *ShowMapHandlerImpl) Handle(params maps.ShowRuntimeMapParams, principal interface{}) middleware.Responder {
104101
m, err := h.Client.Runtime.ShowMapEntries(params.Map)
105102
if err != nil {
106-
e := misc.HandleError(err)
107-
return maps.NewShowRuntimeMapDefault(int(*e.Code)).WithPayload(e)
103+
status := misc.GetHTTPStatusFromErr(err)
104+
return maps.NewShowRuntimeMapDefault(status).WithPayload(misc.SetError(status, err.Error()))
108105
}
109106
if m == nil {
110107
return maps.NewShowRuntimeMapNotFound()
@@ -118,21 +115,10 @@ type AddMapEntryHandlerImpl struct {
118115
}
119116

120117
func (h *AddMapEntryHandlerImpl) Handle(params maps.AddMapEntryParams, principal interface{}) middleware.Responder {
121-
e, _ := h.Client.Runtime.GetMapEntry(params.Map, params.Data.Key)
122-
if e != nil {
123-
msg := fmt.Sprintf("Entry with key %s already exists!", params.Data.Key)
124-
c := misc.ErrHTTPConflict
125-
err := &models.Error{
126-
Message: &msg,
127-
Code: &c,
128-
}
129-
return maps.NewAddMapEntryDefault(maps.AddMapEntryConflictCode).WithPayload(err)
130-
}
131-
132118
err := h.Client.Runtime.AddMapEntry(params.Map, params.Data.Key, params.Data.Value)
133119
if err != nil {
134-
e := misc.HandleError(err)
135-
return maps.NewAddMapEntryDefault(int(*e.Code)).WithPayload(e)
120+
status := misc.GetHTTPStatusFromErr(err)
121+
return maps.NewAddMapEntryDefault(status).WithPayload(misc.SetError(status, err.Error()))
136122
}
137123
return maps.NewAddMapEntryCreated().WithPayload(params.Data)
138124
}
@@ -145,8 +131,8 @@ type GetRuntimeMapEntryHandlerImpl struct {
145131
func (h *GetRuntimeMapEntryHandlerImpl) Handle(params maps.GetRuntimeMapEntryParams, principal interface{}) middleware.Responder {
146132
m, err := h.Client.Runtime.GetMapEntry(params.Map, params.ID)
147133
if err != nil {
148-
e := misc.HandleError(err)
149-
return maps.NewGetRuntimeMapEntryDefault(int(*e.Code)).WithPayload(e)
134+
status := misc.GetHTTPStatusFromErr(err)
135+
return maps.NewGetRuntimeMapEntryDefault(status).WithPayload(misc.SetError(status, err.Error()))
150136
}
151137
if m == nil {
152138
return maps.NewGetRuntimeMapEntryNotFound()
@@ -162,8 +148,8 @@ type ReplaceRuntimeMapEntryHandlerImpl struct {
162148
func (h *ReplaceRuntimeMapEntryHandlerImpl) Handle(params maps.ReplaceRuntimeMapEntryParams, principal interface{}) middleware.Responder {
163149
err := h.Client.Runtime.SetMapEntry(params.Map, params.ID, *params.Data.Value)
164150
if err != nil {
165-
e := misc.HandleError(err)
166-
return maps.NewGetRuntimeMapEntryDefault(int(*e.Code)).WithPayload(e)
151+
status := misc.GetHTTPStatusFromErr(err)
152+
return maps.NewGetRuntimeMapEntryDefault(status).WithPayload(misc.SetError(status, err.Error()))
167153
}
168154

169155
e, err := h.Client.Runtime.GetMapEntry(params.Map, params.ID)
@@ -181,8 +167,8 @@ type DeleteRuntimeMapEntryHandlerImpl struct {
181167
func (h *DeleteRuntimeMapEntryHandlerImpl) Handle(params maps.DeleteRuntimeMapEntryParams, principal interface{}) middleware.Responder {
182168
err := h.Client.Runtime.DeleteMapEntry(params.Map, params.ID)
183169
if err != nil {
184-
e := misc.HandleError(err)
185-
return maps.NewDeleteRuntimeMapEntryDefault(int(*e.Code)).WithPayload(e)
170+
status := misc.GetHTTPStatusFromErr(err)
171+
return maps.NewDeleteRuntimeMapEntryDefault(status).WithPayload(misc.SetError(status, err.Error()))
186172
}
187173
return maps.NewDeleteRuntimeMapEntryNoContent()
188174
}

misc/misc.go

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@ package misc
1717

1818
import (
1919
"encoding/json"
20+
"errors"
21+
"net/http"
2022
"strconv"
2123
"strings"
2224

2325
"github.com/haproxytech/dataplaneapi/haproxy"
2426

2527
"github.com/haproxytech/client-native/v2/configuration"
28+
client_errors "github.com/haproxytech/client-native/v2/errors"
2629
"github.com/haproxytech/models/v2"
2730
)
2831

@@ -50,7 +53,7 @@ func HandleError(err error) *models.Error {
5053
httpCode = ErrHTTPConflict
5154
case configuration.ErrObjectIndexOutOfRange, configuration.ErrValidationError, configuration.ErrBothVersionTransaction,
5255
configuration.ErrNoVersionTransaction, configuration.ErrNoParentSpecified, configuration.ErrParentDoesNotExist,
53-
configuration.ErrTransactionDoesNotExist:
56+
configuration.ErrTransactionDoesNotExist, configuration.ErrGeneralError:
5457
httpCode = ErrHTTPBadRequest
5558
}
5659
return &models.Error{Code: &httpCode, Message: &msg}
@@ -132,3 +135,32 @@ func ParseTimeout(tOut string) *int64 {
132135
}
133136
return nil
134137
}
138+
139+
func GetHTTPStatusFromErr(err error) int {
140+
switch {
141+
case errors.Is(err, client_errors.ErrAlreadyExists):
142+
return http.StatusConflict
143+
case errors.Is(err, client_errors.ErrNotFound):
144+
return http.StatusNotFound
145+
case errors.Is(err, client_errors.ErrGeneral):
146+
return http.StatusBadRequest
147+
default:
148+
return http.StatusInternalServerError
149+
}
150+
}
151+
152+
func SetError(code int, msg string) *models.Error {
153+
return &models.Error{
154+
Code: Int64P(code),
155+
Message: StringP(msg),
156+
}
157+
}
158+
159+
func StringP(s string) *string {
160+
return &s
161+
}
162+
163+
func Int64P(i int) *int64 {
164+
i64 := int64(i)
165+
return &i64
166+
}

0 commit comments

Comments
 (0)