~samwhited/xmpp

d3c2df6a9bc42e869c285658aec73ea675ac29fd — Sam Whited 5 years ago 4eda479
Make StreamError satisfy error

Don't require a child element or text for undefined condition errors
3 files changed, 8 insertions(+), 42 deletions(-)

M stream/example_test.go
M stream/streamerror.go
M stream/streamerror_test.go
M stream/example_test.go => stream/example_test.go +0 -24
@@ 8,8 8,6 @@ import (
	"bytes"
	"encoding/xml"
	"fmt"

	"bitbucket.org/mellium/xmpp"
)

func ExampleStreamError_UnmarshalXML() {


@@ 34,25 32,3 @@ func ExampleStreamError_MarshalXML() {
	//   <not-authorized xmlns="urn:ietf:params:xml:ns:xmpp-streams"></not-authorized>
	// </stream:error>
}

func ExampleUndefinedConditionError() {
	apperr := xmpp.Error{xml.Name{"http://example.org/ns", "app-error"}, ""}
	e := UndefinedConditionError(apperr)
	b, _ := xml.MarshalIndent(e, "", "  ")
	fmt.Println(string(b))
	// Output:
	// <stream:error>
	//   <undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-streams"><app-error xmlns="http://example.org/ns"></app-error></undefined-condition>
	// </stream:error>
}

func ExampleUndefinedConditionError_errorf() {
	apperr := fmt.Errorf("Unknown error")
	e := UndefinedConditionError(apperr)
	b, _ := xml.MarshalIndent(e, "", "  ")
	fmt.Println(string(b))
	// Output:
	// <stream:error>
	//   <undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-streams">Unknown error</undefined-condition>
	// </stream:error>
}

M stream/streamerror.go => stream/streamerror.go +6 -18
@@ 7,8 7,6 @@ package stream
import (
	"encoding/xml"
	"net"

	"bitbucket.org/mellium/xmpp"
)

// A list of stream errors defined in RFC 6120 §4.9.3


@@ 116,6 114,11 @@ var (
	// streams are being closed.
	SystemShutdown = StreamError{Err: "system-shutdown"}

	// UndefinedCondition may be sent when the error condition is not one of those
	// defined by the other conditions in this list; this error condition should
	// be used in conjunction with an application-specific condition.
	UndefinedCondition = StreamError{Err: "undefined-condition"}

	// UnsupportedEncoding may be sent when initiating entity has encoded the
	// stream in an encoding that is not UTF-8.
	UnsupportedEncoding = StreamError{Err: "unsupported-encoding"}


@@ 155,21 158,6 @@ func SeeOtherHostError(addr net.Addr) StreamError {
	return StreamError{"see-other-host", []byte(cdata)}
}

// UndefinedConditionError returns a new undefined-condition stream error with
// the given error as the inner application level error.
func UndefinedConditionError(e error) StreamError {

	var b []byte
	switch e := e.(type) {
	case *xmpp.Error, xmpp.Error:
		b, _ = xml.Marshal(e)
	default:
		b = []byte(e.Error())
	}

	return StreamError{"undefined-condition", b}
}

// A StreamError represents an unrecoverable stream-level error that may include
// character data or arbitrary inner XML.
type StreamError struct {


@@ 185,7 173,7 @@ type StreamError struct {
//     </stream:error>
//
// Error() would return "restricted-xml".
func (e *StreamError) Error() string {
func (e StreamError) Error() string {
	return e.Err
}


M stream/streamerror_test.go => stream/streamerror_test.go +2 -0
@@ 12,7 12,9 @@ import (
)

var _ error = (*StreamError)(nil)
var _ error = StreamError{}
var _ xml.Marshaler = (*StreamError)(nil)
var _ xml.Marshaler = StreamError{}
var _ xml.Unmarshaler = (*StreamError)(nil)

// Both pointers and normal errors should marshal to the same thing.