forked from andlabs/ews
-
Notifications
You must be signed in to change notification settings - Fork 29
/
fault.go
76 lines (65 loc) · 1.48 KB
/
fault.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ews
import (
"encoding/xml"
"io/ioutil"
"net/http"
"strings"
)
func NewError(resp *http.Response) error {
soap, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}
fault, _ := parseSoapFault(string(soap))
if fault == nil {
return &HTTPError{Status: resp.Status, StatusCode: resp.StatusCode}
}
return &SoapError{Fault: fault}
}
type SoapError struct {
Fault *Fault
}
func (s SoapError) Error() string {
return s.Fault.Faultstring
}
type HTTPError struct {
Status string
StatusCode int
}
func (s HTTPError) Error() string {
return s.Status
}
type envelop struct {
XMLName struct{} `xml:"Envelope"`
Body body `xml:"Body"`
}
type body struct {
Fault Fault `xml:"Fault"`
}
type Fault struct {
Faultcode string `xml:"faultcode"`
Faultstring string `xml:"faultstring"`
Detail detail `xml:"detail"`
}
type detail struct {
ResponseCode string `xml:"ResponseCode"`
Message string `xml:"Message"`
MessageXml faultMessageXml `xml:"MessageXml"`
}
type faultMessageXml struct {
LineNumber string `xml:"LineNumber"`
LinePosition string `xml:"LinePosition"`
Violation string `xml:"Violation"`
}
func parseSoapFault(soapMessage string) (*Fault, error) {
var e envelop
err := xml.Unmarshal([]byte(soapMessage), &e)
if err != nil {
return nil, err
}
if len(strings.TrimSpace(e.Body.Fault.Faultcode)) == 0 &&
len(strings.TrimSpace(e.Body.Fault.Faultstring)) == 0 {
return nil, nil
}
return &e.Body.Fault, nil
}