comparison pkg/soap/soap.go @ 3321:bd1385c00b59

Applied Tom's patch (with little changes) to make SOAP errors more verbose.
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 20 May 2019 11:00:30 +0200
parents 6a44a89ffb51
children 96dbfcc614be
comparison
equal deleted inserted replaced
3320:1473e9e7cd0c 3321:bd1385c00b59
228 228
229 func (s *SOAPClient) AddHeader(header interface{}) { 229 func (s *SOAPClient) AddHeader(header interface{}) {
230 s.headers = append(s.headers, header) 230 s.headers = append(s.headers, header)
231 } 231 }
232 232
233 func shorten(s string) string {
234 if len(s) < 40 {
235 return s
236 }
237 return fmt.Sprintf("%.40q...", s)
238 }
239
233 func (s *SOAPClient) Call(soapAction string, request, response interface{}) error { 240 func (s *SOAPClient) Call(soapAction string, request, response interface{}) error {
234 envelope := SOAPEnvelope{} 241 envelope := SOAPEnvelope{}
235 242
236 if s.headers != nil && len(s.headers) > 0 { 243 if s.headers != nil && len(s.headers) > 0 {
237 soapHeader := &SOAPHeader{Items: make([]interface{}, len(s.headers))} 244 soapHeader := &SOAPHeader{Items: make([]interface{}, len(s.headers))}
293 300
294 res, err := client.Do(req) 301 res, err := client.Do(req)
295 if err != nil { 302 if err != nil {
296 return err 303 return err
297 } 304 }
298 if res.StatusCode < http.StatusOK || res.StatusCode > 299 {
299 return fmt.Errorf(
300 "HTTP error: %d (%s)", res.StatusCode, http.StatusText(res.StatusCode))
301 }
302 defer res.Body.Close() 305 defer res.Body.Close()
303 306
304 rawbody, err := ioutil.ReadAll(res.Body) 307 rawbody, err := ioutil.ReadAll(res.Body)
305 if err != nil { 308 if err != nil {
306 return err 309 return err
307 } 310 }
308 if len(rawbody) == 0 { 311
309 log.Println("empty response") 312 if res.StatusCode < http.StatusOK || res.StatusCode > 299 {
310 return nil 313 short := shorten(string(rawbody))
311 } 314 log.Printf("error: response from SOAP service:\n%s", short)
312 315 return fmt.Errorf("Error requesting SOAP service: %d (%s): %s",
313 //log.Println(string(rawbody)) 316 res.StatusCode, http.StatusText(res.StatusCode), short)
317 }
318
314 respEnvelope := new(SOAPEnvelope) 319 respEnvelope := new(SOAPEnvelope)
315 respEnvelope.Body = SOAPBody{Content: response} 320 respEnvelope.Body = SOAPBody{Content: response}
316 err = xml.Unmarshal(rawbody, respEnvelope) 321 err = xml.Unmarshal(rawbody, respEnvelope)
317 if err != nil { 322 if err != nil {
318 return err 323 return err
319 } 324 }
320 325
321 fault := respEnvelope.Body.Fault 326 return respEnvelope.Body.Fault
322 if fault != nil { 327 }
323 return fault
324 }
325
326 return nil
327 }