Add ErrFault error marker.
This commit is contained in:
parent
39b68767e6
commit
c43feb1f5a
@ -3,10 +3,14 @@ package envelope
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ErrFault can be used as a target with errors.Is.
|
||||||
|
var ErrFault error = errors.New("xml fault")
|
||||||
|
|
||||||
// FaultDetail carries XML-encoded application-specific Fault details.
|
// FaultDetail carries XML-encoded application-specific Fault details.
|
||||||
type FaultDetail struct {
|
type FaultDetail struct {
|
||||||
Raw []byte `xml:",innerxml"`
|
Raw []byte `xml:",innerxml"`
|
||||||
@ -24,6 +28,10 @@ func (fe *Fault) Error() string {
|
|||||||
return fmt.Sprintf("SOAP fault code=%s: %s", fe.Code, fe.String)
|
return fmt.Sprintf("SOAP fault code=%s: %s", fe.Code, fe.String)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (fe *Fault) Is(target error) bool {
|
||||||
|
return target == ErrFault
|
||||||
|
}
|
||||||
|
|
||||||
// Various "constant" bytes used in the written envelope.
|
// Various "constant" bytes used in the written envelope.
|
||||||
var (
|
var (
|
||||||
envOpen = []byte(xml.Header + `<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>`)
|
envOpen = []byte(xml.Header + `<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><s:Body>`)
|
||||||
|
@ -3,6 +3,9 @@ package envelope
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"errors"
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
@ -76,3 +79,36 @@ s:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
|
|||||||
t.Errorf("want %+v, got %+v", wantFault, gotFault)
|
t.Errorf("want %+v, got %+v", wantFault, gotFault)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFault(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
err error
|
||||||
|
wantIs bool
|
||||||
|
}{
|
||||||
|
{"plain fault", &Fault{Code: "code"}, true},
|
||||||
|
{"wrapped fault", fmt.Errorf("wrapper: %w", &Fault{Code: "code"}), true},
|
||||||
|
{"other error", io.EOF, false},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test // copy for closure
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
if got, want := errors.Is(test.err, ErrFault), test.wantIs; got != want {
|
||||||
|
t.Errorf("got errors.Is(%v, ErrFault)=>%t, want %t", test.err, got, want)
|
||||||
|
}
|
||||||
|
if !test.wantIs {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var fault *Fault
|
||||||
|
if got, want := errors.As(test.err, &fault), true; got != want {
|
||||||
|
t.Fatalf("got errors.As(%v, ...)=>%t, want %t", test.err, got, want)
|
||||||
|
}
|
||||||
|
|
||||||
|
if got, want := fault.Code, "code"; got != want {
|
||||||
|
t.Errorf("got fault.Code=%q, want %q", got, want)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user