~samwhited/xmpp

326541d5d47c37eb6f6b6acae52a261f8cf7f5d7 — Sam Whited 4 years ago c25a628
stanza: rename "StanzaError" to "Error"

Now that StanzaError has been moved into the stanza package it
introduced unnecessary stutter.
4 files changed, 36 insertions(+), 36 deletions(-)

M bind.go
M session.go
R stanza/{errors.go => error.go}
R stanza/{errors_test.go => error_test.go}
M bind.go => bind.go +2 -2
@@ 78,7 78,7 @@ func BindResource() StreamFeature {
				Bind struct {
					JID *jid.JID `xml:"jid"`
				} `xml:"urn:ietf:params:xml:ns:xmpp-bind bind"`
				Err stanza.StanzaError `xml:"error"`
				Err stanza.Error `xml:"error"`
			}{}
			switch start.Name {
			case xml.Name{Space: ns.Client, Local: "iq"}:


@@ 97,7 97,7 @@ func BindResource() StreamFeature {
			case resp.Type == stanza.ErrorIQ:
				return mask, nil, resp.Err
			default:
				return mask, nil, stanza.StanzaError{Condition: stanza.BadRequest}
				return mask, nil, stanza.Error{Condition: stanza.BadRequest}
			}
			return Ready, nil, nil
		},

M session.go => session.go +3 -3
@@ 111,8 111,8 @@ func NewSession(ctx context.Context, config *Config, rw io.ReadWriter) (*Session

// Serve decodes incoming XML tokens from the connection and delegates handling
// them to the provided handler.
// If an error is returned from the handler and it is of type stanza.StanzaError
// or stream.Error, the error is marshaled and sent over the XML stream. If any
// If an error is returned from the handler and it is of type stanza.Error or
// stream.Error, the error is marshaled and sent over the XML stream. If any
// other error type is returned, it is marshaled as an undefined-condition
// StreamError. If a stream error is received while serving it is not passed to
// the handler. Instead, Serve unmarshals the error, closes the session, and


@@ 231,7 231,7 @@ func (s *Session) handleInputStream(handler Handler) error {
			}
			if err = handler.HandleXMPP(s, &t); err != nil {
				switch err.(type) {
				case stanza.StanzaError:
				case stanza.Error:
					err = s.Encoder().Encode(err)
					if err != nil {
						return err

R stanza/errors.go => stanza/error.go +5 -5
@@ 86,9 86,9 @@ const (
	UnexpectedRequest     condition = "unexpected-request"
)

// StanzaError is an implementation of error intended to be marshalable and
// Error is an implementation of error intended to be marshalable and
// unmarshalable as XML.
type StanzaError struct {
type Error struct {
	XMLName   xml.Name
	By        *jid.JID
	Type      errorType


@@ 99,7 99,7 @@ type StanzaError struct {

// Error satisfies the error interface and returns the text if set, or the
// condition otherwise.
func (se StanzaError) Error() string {
func (se Error) Error() string {
	if se.Text != "" {
		return se.Text
	}


@@ 107,7 107,7 @@ func (se StanzaError) Error() string {
}

// MarshalXML satisfies the xml.Marshaler interface for StanzaError.
func (se StanzaError) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
func (se Error) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
	start = xml.StartElement{
		Name: xml.Name{Space: ``, Local: "error"},
		Attr: []xml.Attr{},


@@ 155,7 155,7 @@ func (se StanzaError) MarshalXML(e *xml.Encoder, start xml.StartElement) (err er
}

// UnmarshalXML satisfies the xml.Unmarshaler interface for StanzaError.
func (se *StanzaError) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
func (se *Error) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	decoded := struct {
		Condition struct {
			XMLName xml.Name

R stanza/errors_test.go => stanza/error_test.go +26 -26
@@ 14,18 14,18 @@ import (
)

var (
	_ error        = (*StanzaError)(nil)
	_ error        = StanzaError{}
	_ error        = (*Error)(nil)
	_ error        = Error{}
	_ fmt.Stringer = (*errorType)(nil)
	_ fmt.Stringer = Auth
)

func TestErrorReturnsCondition(t *testing.T) {
	s := StanzaError{Condition: "leprosy"}
	s := Error{Condition: "leprosy"}
	if string(s.Condition) != s.Error() {
		t.Errorf("Expected stanza error to return condition `leprosy` but got %s", s.Error())
	}
	s = StanzaError{Condition: "nope", Text: "Text"}
	s = Error{Condition: "nope", Text: "Text"}
	if s.Text != s.Error() {
		t.Errorf("Expected stanza error to return text `Text` but got %s", s.Error())
	}


@@ 33,16 33,16 @@ func TestErrorReturnsCondition(t *testing.T) {

func TestMarshalStanzaError(t *testing.T) {
	for _, data := range []struct {
		se  StanzaError
		se  Error
		xml string
		err bool
	}{
		{StanzaError{}, "", true},
		{StanzaError{Condition: UnexpectedRequest}, `<error type="cancel"><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		{StanzaError{Type: Cancel, Condition: UnexpectedRequest}, `<error type="cancel"><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		{StanzaError{Type: Wait, Condition: UndefinedCondition}, `<error type="wait"><undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></undefined-condition></error>`, false},
		{StanzaError{Type: Modify, By: jid.MustParse("test@example.net"), Condition: SubscriptionRequired}, `<error type="modify" by="test@example.net"><subscription-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></subscription-required></error>`, false},
		{StanzaError{Type: Continue, Condition: ServiceUnavailable, Text: "test"}, `<error type="continue"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></service-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="und">test</text></error>`, false},
		{Error{}, "", true},
		{Error{Condition: UnexpectedRequest}, `<error type="cancel"><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		{Error{Type: Cancel, Condition: UnexpectedRequest}, `<error type="cancel"><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`, false},
		{Error{Type: Wait, Condition: UndefinedCondition}, `<error type="wait"><undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></undefined-condition></error>`, false},
		{Error{Type: Modify, By: jid.MustParse("test@example.net"), Condition: SubscriptionRequired}, `<error type="modify" by="test@example.net"><subscription-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></subscription-required></error>`, false},
		{Error{Type: Continue, Condition: ServiceUnavailable, Text: "test"}, `<error type="continue"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></service-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="und">test</text></error>`, false},
	} {
		b, err := xml.Marshal(data.se)
		switch {


@@ 64,36 64,36 @@ func TestUnmarshalStanzaError(t *testing.T) {
	for _, data := range []struct {
		xml  string
		lang language.Tag
		se   StanzaError
		se   Error
		err  bool
	}{
		{"", language.Und, StanzaError{}, true},
		{"", language.Und, Error{}, true},
		{`<error><unexpected-request xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></unexpected-request></error>`,
			language.Und, StanzaError{Condition: UnexpectedRequest}, false},
			language.Und, Error{Condition: UnexpectedRequest}, false},
		{`<error type="cancel"><registration-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></registration-required></error>`,
			language.Und, StanzaError{Condition: RegistrationRequired}, false},
			language.Und, Error{Condition: RegistrationRequired}, false},
		{`<error type="cancel"><redirect xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></redirect></error>`,
			language.Und, StanzaError{Type: Cancel, Condition: Redirect}, false},
			language.Und, Error{Type: Cancel, Condition: Redirect}, false},
		{`<error type="wait"><undefined-condition xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></undefined-condition></error>`,
			language.Und, StanzaError{Type: Wait, Condition: UndefinedCondition}, false},
			language.Und, Error{Type: Wait, Condition: UndefinedCondition}, false},
		{`<error type="modify" by="test@example.net"><subscription-required xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></subscription-required></error>`,
			language.Und, StanzaError{Type: Modify, By: jid.MustParse("test@example.net"), Condition: SubscriptionRequired}, false},
			language.Und, Error{Type: Modify, By: jid.MustParse("test@example.net"), Condition: SubscriptionRequired}, false},
		{`<error type="continue"><service-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></service-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="und">test</text></error>`,
			language.Und, StanzaError{Type: Continue, Condition: ServiceUnavailable, Text: "test"}, false},
			language.Und, Error{Type: Continue, Condition: ServiceUnavailable, Text: "test"}, false},
		{`<error type="auth"><resource-constraint xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></resource-constraint><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">test</text></error>`,
			language.Und, StanzaError{Type: Auth, Condition: ResourceConstraint, Text: "test", Lang: language.English}, false},
			language.Und, Error{Type: Auth, Condition: ResourceConstraint, Text: "test", Lang: language.English}, false},
		{`<error type="auth"><resource-constraint xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></resource-constraint><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">test</text><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="de">German</text></error>`,
			language.German, StanzaError{Type: Auth, Condition: ResourceConstraint, Text: "German", Lang: language.German}, false},
			language.German, Error{Type: Auth, Condition: ResourceConstraint, Text: "German", Lang: language.German}, false},
		{`<error type="auth"><remote-server-timeout xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></remote-server-timeout><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="en">test</text><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="es">Spanish</text></error>`,
			language.LatinAmericanSpanish, StanzaError{Type: Auth, Condition: RemoteServerTimeout, Text: "Spanish", Lang: language.Spanish}, false},
			language.LatinAmericanSpanish, Error{Type: Auth, Condition: RemoteServerTimeout, Text: "Spanish", Lang: language.Spanish}, false},
		{`<error by=""><remote-server-not-found xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></remote-server-not-found></error>`,
			language.Und, StanzaError{By: &jid.JID{}, Condition: RemoteServerNotFound}, false},
			language.Und, Error{By: &jid.JID{}, Condition: RemoteServerNotFound}, false},
		{`<error><other xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></other></error>`,
			language.Und, StanzaError{Condition: condition("other")}, false},
			language.Und, Error{Condition: condition("other")}, false},
		{`<error><recipient-unavailable xmlns="urn:ietf:params:xml:ns:xmpp-stanzas"></recipient-unavailable><text xmlns="urn:ietf:params:xml:ns:xmpp-stanzas" xml:lang="ac-u">test</text></error>`,
			language.Und, StanzaError{Condition: RecipientUnavailable}, false},
			language.Und, Error{Condition: RecipientUnavailable}, false},
	} {
		se2 := StanzaError{Lang: data.lang}
		se2 := Error{Lang: data.lang}
		err := xml.Unmarshal([]byte(data.xml), &se2)
		j1, j2 := data.se.By, se2.By
		data.se.By = nil