Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add fault string to error message #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 32 additions & 8 deletions soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"net/url"
Expand Down Expand Up @@ -187,6 +186,13 @@ func (c *Client) Do(req *Request) (res *Response, err error) {

b, err := p.doRequest(c.Definitions.Services[0].Ports[0].SoapAddresses[0].Location)
if err != nil {
var soapError SoapFaultEnvelope
decoder := xml.NewDecoder(bytes.NewReader(b))
decoder.CharsetReader = charset.NewReaderLabel
decodeErr := decoder.Decode(&soapError)
if decodeErr == nil && soapError.Body.Fault.Faultcode != "" {
err = fmt.Errorf("%w: %s (%s)", err, soapError.Body.Fault.Faultcode, soapError.Body.Fault.Faultstring)
}
return nil, ErrorWithPayload{err, p.Payload}
}

Expand Down Expand Up @@ -262,16 +268,14 @@ func (p *process) doRequest(url string) ([]byte, error) {
}

if resp.StatusCode < 200 || resp.StatusCode >= 400 {
if !(p.Client.config != nil && p.Client.config.Dump) {
_, err := io.Copy(ioutil.Discard, resp.Body)
if err != nil {
return nil, err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unexpected status code - failed to read body: %s - %w", resp.Status, err)
}
return nil, errors.New("unexpected status code: " + resp.Status)
return body, errors.New("unexpected status code: " + resp.Status)
}

return ioutil.ReadAll(resp.Body)
return io.ReadAll(resp.Body)
}

func (p *process) httpClient() *http.Client {
Expand Down Expand Up @@ -313,3 +317,23 @@ type SoapBody struct {
XMLName struct{} `xml:"Body"`
Contents []byte `xml:",innerxml"`
}

// SoapEnvelope fault in body struct
type SoapFaultEnvelope struct {
XMLName struct{} `xml:"Envelope"`
Header SoapHeader
Body SoapFaultBody
}

// SoapFaultBody struct
type SoapFaultBody struct {
XMLName struct{} `xml:"Body"`
Fault SoapFault
}

// SoapFault struct
type SoapFault struct {
XMLName struct{} `xml:"Fault"`
Faultcode string `xml:"faultcode"`
Faultstring string `xml:"faultstring"`
}
9 changes: 9 additions & 0 deletions soap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"net/http"
"regexp"
"strings"
"testing"
)

Expand Down Expand Up @@ -184,6 +185,14 @@ func TestClient_Call(t *testing.T) {
t.Errorf("error: %+v", rw)
}

_, err = soap.Call("Whatever", Params{"DomainName": "google.com"})
if err == nil {
t.Errorf("error expected but nothing got.")
}
if !strings.Contains(err.Error(), "Unable to handle request without a valid action parameter") {
t.Errorf("expected error message to contain fault message")
}

c := &Client{}
res, err = c.Call("", Params{})
if err == nil {
Expand Down