1616package handlers
1717
1818import (
19+ "encoding/json"
1920 "fmt"
21+ "net/http"
2022 "strconv"
2123 "strings"
2224
@@ -61,7 +63,11 @@ func (h *GetRawConfigurationHandlerImpl) Handle(params configuration.GetHAProxyC
6163 _ , clusterVersion , md5Hash , data , err := cfg .GetRawConfigurationWithClusterData (t , v )
6264 if err != nil {
6365 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 ))
6571 }
6672 cVersion := ""
6773 if clusterVersion != 0 {
@@ -99,25 +105,41 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
99105 code := misc .ErrHTTPBadRequest
100106 msg := "invalid configuration: no newline character found"
101107 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 ))
103113 }
104114
105115 cfg , err := h .Client .Configuration ()
106116 if err != nil {
107117 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 ))
109123 }
110124
111125 err = cfg .PostRawConfiguration (& params .Data , v , skipVersion , onlyValidate )
112126 if err != nil {
113127 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 ))
115133 }
116134
117135 _ , clusterVersion , md5Hash , data , err := cfg .GetRawConfigurationWithClusterData ("" , 0 )
118136 if err != nil {
119137 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 ))
121143 }
122144
123145 cVersion := ""
@@ -132,7 +154,11 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
132154 if params .XRuntimeActions != nil {
133155 if err = executeRuntimeActions (* params .XRuntimeActions , h .Client ); err != nil {
134156 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 ))
136162 }
137163 }
138164 return configuration .NewPostHAProxyConfigurationCreated ().WithPayload (data ).WithClusterVersion (cVersion ).WithConfigurationChecksum (md5Hash )
@@ -143,7 +169,11 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
143169 callbackNeeded , reconfigureFunc , err = cn .ReconfigureRuntime (h .Client )
144170 if err != nil {
145171 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 ))
147177 }
148178 if callbackNeeded {
149179 err = h .ReloadAgent .ForceReloadWithCallback (reconfigureFunc )
@@ -152,14 +182,22 @@ func (h *PostRawConfigurationHandlerImpl) Handle(params configuration.PostHAProx
152182 }
153183 if err != nil {
154184 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 ))
156190 }
157191 return configuration .NewPostHAProxyConfigurationCreated ().WithPayload (data ).WithClusterVersion (cVersion ).WithConfigurationChecksum (md5Hash )
158192 }
159193 callbackNeeded , reconfigureFunc , err := cn .ReconfigureRuntime (h .Client )
160194 if err != nil {
161195 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 ))
163201 }
164202
165203 var rID string
0 commit comments