16
16
package handlers
17
17
18
18
import (
19
+ "encoding/json"
19
20
"fmt"
21
+ "net/http"
20
22
"strconv"
21
23
"strings"
22
24
@@ -61,7 +63,11 @@ func (h *GetRawConfigurationHandlerImpl) Handle(params configuration.GetHAProxyC
61
63
_ , clusterVersion , md5Hash , data , err := cfg .GetRawConfigurationWithClusterData (t , v )
62
64
if err != nil {
63
65
e := misc .HandleError (err )
64
- return configuration .NewGetHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
66
+ errorStr , marshallError := json .Marshal (e )
67
+ if marshallError != nil {
68
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
69
+ }
70
+ return configuration .NewGetHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
65
71
}
66
72
cVersion := ""
67
73
if clusterVersion != 0 {
@@ -99,25 +105,41 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
99
105
code := misc .ErrHTTPBadRequest
100
106
msg := "invalid configuration: no newline character found"
101
107
e := & models.Error {Code : & code , Message : & msg }
102
- return configuration .NewPostHAProxyConfigurationBadRequest ().WithPayload (e )
108
+ errorStr , marshallError := json .Marshal (e )
109
+ if marshallError != nil {
110
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
111
+ }
112
+ return configuration .NewPostHAProxyConfigurationBadRequest ().WithPayload (string (errorStr ))
103
113
}
104
114
105
115
cfg , err := h .Client .Configuration ()
106
116
if err != nil {
107
117
e := misc .HandleError (err )
108
- return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
118
+ errorStr , marshallError := json .Marshal (e )
119
+ if marshallError != nil {
120
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
121
+ }
122
+ return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
109
123
}
110
124
111
125
err = cfg .PostRawConfiguration (& params .Data , v , skipVersion , onlyValidate )
112
126
if err != nil {
113
127
e := misc .HandleError (err )
114
- return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
128
+ errorStr , marshallError := json .Marshal (e )
129
+ if marshallError != nil {
130
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
131
+ }
132
+ return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
115
133
}
116
134
117
135
_ , clusterVersion , md5Hash , data , err := cfg .GetRawConfigurationWithClusterData ("" , 0 )
118
136
if err != nil {
119
137
e := misc .HandleError (err )
120
- return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
138
+ errorStr , marshallError := json .Marshal (e )
139
+ if marshallError != nil {
140
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
141
+ }
142
+ return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
121
143
}
122
144
123
145
cVersion := ""
@@ -132,7 +154,11 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
132
154
if params .XRuntimeActions != nil {
133
155
if err = executeRuntimeActions (* params .XRuntimeActions , h .Client ); err != nil {
134
156
e := misc .HandleError (err )
135
- return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
157
+ errorStr , marshallError := json .Marshal (e )
158
+ if marshallError != nil {
159
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
160
+ }
161
+ return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
136
162
}
137
163
}
138
164
return configuration .NewPostHAProxyConfigurationCreated ().WithPayload (data ).WithClusterVersion (cVersion ).WithConfigurationChecksum (md5Hash )
@@ -143,7 +169,11 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
143
169
callbackNeeded , reconfigureFunc , err = cn .ReconfigureRuntime (h .Client )
144
170
if err != nil {
145
171
e := misc .HandleError (err )
146
- return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
172
+ errorStr , marshallError := json .Marshal (e )
173
+ if marshallError != nil {
174
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
175
+ }
176
+ return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
147
177
}
148
178
if callbackNeeded {
149
179
err = h .ReloadAgent .ForceReloadWithCallback (reconfigureFunc )
@@ -152,14 +182,22 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
152
182
}
153
183
if err != nil {
154
184
e := misc .HandleError (err )
155
- return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
185
+ errorStr , marshallError := json .Marshal (e )
186
+ if marshallError != nil {
187
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
188
+ }
189
+ return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
156
190
}
157
191
return configuration .NewPostHAProxyConfigurationCreated ().WithPayload (data ).WithClusterVersion (cVersion ).WithConfigurationChecksum (md5Hash )
158
192
}
159
193
callbackNeeded , reconfigureFunc , err := cn .ReconfigureRuntime (h .Client )
160
194
if err != nil {
161
195
e := misc .HandleError (err )
162
- return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (e )
196
+ errorStr , marshallError := json .Marshal (e )
197
+ if marshallError != nil {
198
+ configuration .NewPostHAProxyConfigurationDefault (http .StatusInternalServerError ).WithPayload (marshallError .Error ())
199
+ }
200
+ return configuration .NewPostHAProxyConfigurationDefault (int (* e .Code )).WithPayload (string (errorStr ))
163
201
}
164
202
165
203
var rID string
0 commit comments