From 5cdb2b0e5948cb983a19449998e9016b6ed8d3e9 Mon Sep 17 00:00:00 2001 From: Grant Gongaware Date: Wed, 1 Mar 2023 09:08:29 -0800 Subject: [PATCH] Return enhanced Error struct with Request and Response if available --- example/example.go | 4 ++-- example/gen/gen.go | 2 +- soap/soap.go | 26 ++++++++++++++++++-------- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/example/example.go b/example/example.go index c65cef17..ab1b9403 100644 --- a/example/example.go +++ b/example/example.go @@ -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() { diff --git a/example/gen/gen.go b/example/gen/gen.go index 5dbfc6fa..a94d20fd 100644 --- a/example/gen/gen.go +++ b/example/gen/gen.go @@ -5,7 +5,7 @@ import ( "encoding/xml" "time" - "github.com/hooklift/gowsdl/soap" + "github.com/telmate/gowsdl/soap" ) // against "unused imports" diff --git a/soap/soap.go b/soap/soap.go index 2b656920..8666c09b 100644 --- a/soap/soap.go +++ b/soap/soap.go @@ -5,6 +5,7 @@ import ( "context" "crypto/tls" "encoding/xml" + "fmt" "io/ioutil" "net" "net/http" @@ -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 { @@ -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) } @@ -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") @@ -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