From f4bc539cd7c6868942c5187e4dfabe3734f37930 Mon Sep 17 00:00:00 2001 From: Martin/Geno Date: Sun, 11 Feb 2018 15:11:52 +0100 Subject: [PATCH] improve stanze error handling --- client/client.go | 13 ++++++---- daemon/tester/bot.go | 2 +- messages/error.go | 47 ++++++++++++++++++++++++++++++++----- server/toclient/register.go | 5 ++-- 4 files changed, 53 insertions(+), 14 deletions(-) diff --git a/client/client.go b/client/client.go index f52d1c1..95895b7 100644 --- a/client/client.go +++ b/client/client.go @@ -149,10 +149,15 @@ func (client *Client) connect(password string) error { if err := client.ReadElement(&iq); err != nil { return err } - if iq.Bind == nil { + if iq.Error != nil { + if iq.Error.Type == messages.ErrorClientTypeCancel && iq.Error.ServiceUnavailable != nil { + //TODO binding service unavailable + } else { + return errors.New(fmt.Sprintf("recv error on iq>bind: %s[%s]: %s -> %v", iq.Error.Code, iq.Error.Type, iq.Error.Text, iq.Error.Other)) + } + } else if iq.Bind == nil { return errors.New(" result missing ") - } - if iq.Bind.JID != nil { + } else if iq.Bind.JID != nil { client.JID.Local = iq.Bind.JID.Local client.JID.Domain = iq.Bind.JID.Domain client.JID.Resource = iq.Bind.JID.Resource @@ -160,7 +165,7 @@ func (client *Client) connect(password string) error { return errors.New(fmt.Sprintf("%v", iq.Other)) } // set status - client.Send(&messages.PresenceClient{Show: messages.ShowTypeXA, Status: "online"}) + err = client.Send(&messages.PresenceClient{Show: messages.ShowTypeXA, Status: "online"}) return err } diff --git a/daemon/tester/bot.go b/daemon/tester/bot.go index e516209..b13c39c 100644 --- a/daemon/tester/bot.go +++ b/daemon/tester/bot.go @@ -82,7 +82,7 @@ func (t *Tester) StartBot(status *Status) { } logCTX = logCTX.WithField("from", msg.From.Full()).WithField("msg-recv", msg.Body) if msg.Error != nil { - logCTX.Debugf("recv msg with error %s: %s", msg.Error.Code, msg.Error.Text) + logCTX.Debugf("recv msg with error %s[%s]: %s -> %v -> %v", msg.Error.Code, msg.Error.Type, msg.Error.Text, msg.Error.StanzaErrorGroup, msg.Error.Other) continue } diff --git a/messages/error.go b/messages/error.go index 0e03ef2..3fb73a5 100644 --- a/messages/error.go +++ b/messages/error.go @@ -9,12 +9,47 @@ type StreamError struct { Any xml.Name `xml:",any"` } +type ErrorClientType string + +const ( + ErrorClientTypeAuth ErrorClientType = "auth" + ErrorClientTypeCancel ErrorClientType = "cancel" + ErrorClientTypeContinue ErrorClientType = "continue" + ErrorClientTypeModify ErrorClientType = "motify" + ErrorClientTypeWait ErrorClientType = "wait" +) + +type StanzaErrorGroup struct { + BadRequest *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas bad-request"` + Conflict *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas conflict"` + FeatureNotImplemented *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas feature-not-implemented"` + Forbidden *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas forbidden"` + Gone string `xml:"urn:ietf:params:xml:ns:xmpp-stanzas gone"` + InternalServerError *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas internal-server-error"` + ItemNotFound *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas item-not-found"` + JIDMalformed *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas jid-malformed"` + NotAcceptable *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas not-acceptable"` + NotAllowed *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas not-allowed"` + NotAuthorized *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas not-authorized"` + PolicyViolation *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas policy-violation"` + RecipientUnavailable *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas recipient-unavailable"` + Redirect string `xml:"urn:ietf:params:xml:ns:xmpp-stanzas redirect"` + RegistrationRequired *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas registration-required"` + RemoteServerNotFound *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas remote-server-not-found"` + RemoteServerTimeout *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas remote-server-timeout"` + ResourceConstraint *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas resource-constraint"` + ServiceUnavailable *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas service-unavailable"` + SubscriptionRequired *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas subscription-required"` + UndefinedCondition *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas undefined-condition"` + UnexpectedRequest *xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-stanzas unexpected-request"` +} + // ErrorClient element type ErrorClient struct { - XMLName xml.Name `xml:"jabber:client error"` - Code string `xml:"code,attr"` - Type string `xml:"type,attr"` - Text string `xml:"text"` - - Any xml.Name `xml:",any"` + XMLName xml.Name `xml:"jabber:client error"` + Code string `xml:"code,attr"` + Type ErrorClientType `xml:"type,attr"` + Text string `xml:"text"` + Other []XMLElement `xml:",any"` + StanzaErrorGroup } diff --git a/server/toclient/register.go b/server/toclient/register.go index 9cdbdab..1703d1c 100644 --- a/server/toclient/register.go +++ b/server/toclient/register.go @@ -115,9 +115,8 @@ func (state *RegisterRequest) Process() state.State { Error: &messages.ErrorClient{ Code: "409", Type: "cancel", - Any: xml.Name{ - Local: "conflict", - Space: "urn:ietf:params:xml:ns:xmpp-stanzas", + StanzaErrorGroup: messages.StanzaErrorGroup{ + Conflict: &xml.Name{}, }, }, })