Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 18 additions & 17 deletions soaptrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"bytes"
"encoding/xml"
"fmt"
"io/ioutil"
"io"
"net/http"
"strings"
)
Expand Down Expand Up @@ -61,22 +61,13 @@ func (sf SoapFault) Error() string {
// ParseFault attempts to parse a Soap Fault from an http.Response. If a fault is found, it will return an error
// of type SoapFault, otherwise it will return nil
func ParseFault(resp *http.Response) error {
// read the response, but don't close it
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

// replace the read closer that we just used
// TODO: theres a more elegant way, maybe a MultiReader?
resp.Body = ioutil.NopCloser(bytes.NewBuffer(b))

reader := bytes.NewReader(b)
d := xml.NewDecoder(reader)
var buf bytes.Buffer
d := xml.NewDecoder(io.TeeReader(resp.Body, &buf))

var start xml.StartElement
fault := &SoapFault{}
fault := &SoapFault{Response: resp}
found := false
depth := 0

// iterate through the tokens
for {
Expand All @@ -89,21 +80,31 @@ func ParseFault(resp *http.Response) error {
switch t := tok.(type) {
case xml.StartElement:
start = t.Copy()
depth++
if depth > 2 { // don't descend beyond Envelope>Body>Fault
break
}
case xml.EndElement:
start = xml.StartElement{}
depth--
case xml.CharData:
key := strings.ToLower(start.Name.Local)
// fault was found, capture the values and mark as found
if key == "faultcode" {
switch strings.ToLower(start.Name.Local) {
case "faultcode":
found = true
fault.FaultCode = string(t)
} else if key == "faultstring" {
case "faultstring":
found = true
fault.FaultString = string(t)
}
}
}

resp.Body = struct {
io.Reader
io.Closer
}{io.MultiReader(bytes.NewReader(buf.Bytes()), resp.Body), resp.Body}

if found {
fault.Response = resp
return fault
Expand Down