Skip to content

Commit

Permalink
Return enhanced Error struct with Request and Response if available
Browse files Browse the repository at this point in the history
  • Loading branch information
Grant Gongaware committed Mar 1, 2023
1 parent cb210bf commit 5cdb2b0
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions example/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"log"
"time"

"github.com/hooklift/gowsdl/example/gen"
"github.com/hooklift/gowsdl/soap"
"github.com/telmate/gowsdl/example/gen"
"github.com/telmate/gowsdl/soap"
)

func ExampleBasicUsage() {
Expand Down
2 changes: 1 addition & 1 deletion example/gen/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"encoding/xml"
"time"

"github.com/hooklift/gowsdl/soap"
"github.com/telmate/gowsdl/soap"
)

// against "unused imports"
Expand Down
26 changes: 18 additions & 8 deletions soap/soap.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/tls"
"encoding/xml"
"fmt"
"io/ioutil"
"net"
"net/http"
Expand All @@ -24,6 +25,15 @@ type SOAPBody struct {
Content interface{} `xml:",omitempty"`
}

type Error struct {
Err error
Req *http.Request
Res *http.Response
}

func (e *Error) Unwrap() error { return e.Err }
func (e *Error) Error() string { return fmt.Sprintf("Soap Error: %s", e.Err) }

// UnmarshalXML unmarshals SOAPBody xml
func (b *SOAPBody) UnmarshalXML(d *xml.Decoder, _ xml.StartElement) error {
if b.Content == nil {
Expand Down Expand Up @@ -213,7 +223,7 @@ func WithHTTPHeaders(headers map[string]string) Option {
}
}

//HTTPClient - mockable interface
// HTTPClient - mockable interface
type HTTPClient interface {
Do(req *http.Request) (*http.Response, error)
}
Expand Down Expand Up @@ -277,13 +287,13 @@ func (s *Client) Call(ctx context.Context, soapAction string, request, response

req, err := http.NewRequest("POST", s.url, buffer)
if err != nil {
return err
return &Error{err, req, nil}
}
if s.opts.auth != nil {
req.SetBasicAuth(s.opts.auth.Login, s.opts.auth.Password)
}

req.WithContext(ctx)
req = req.WithContext(ctx)
req.Header.Add("Content-Type", "text/xml; charset=\"utf-8\"")
req.Header.Add("SOAPAction", soapAction)
req.Header.Set("User-Agent", "gowsdl/0.1")
Expand All @@ -297,28 +307,28 @@ func (s *Client) Call(ctx context.Context, soapAction string, request, response

res, err := s.HClient.Do(req)
if err != nil {
return err
return &Error{err, req, res}
}
defer res.Body.Close()

rawbody, err := ioutil.ReadAll(res.Body)
if err != nil {
return err
return &Error{err, req, res}
}
if len(rawbody) == 0 {
return nil
return &Error{nil, req, res}
}

respEnvelope := new(SOAPEnvelope)
respEnvelope.Body = SOAPBody{Content: response}
err = xml.Unmarshal(rawbody, respEnvelope)
if err != nil {
return err
return &Error{err, req, res}
}

fault := respEnvelope.Body.Fault
if fault != nil {
return fault
return &Error{fault, req, res}
}

return nil
Expand Down

0 comments on commit 5cdb2b0

Please sign in to comment.