~samwhited/xmpp

ref: d138ede52e6d7a941cb5fa6c135d9792f1a2a832 xmpp/errors.go -rw-r--r-- 6.7 KiB
d138ede5Sam Whited Add marshaling of stanza errors (and tests) 5 years ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// Copyright 2016 Sam Whited.
// Use of this source code is governed by the BSD 2-clause license that can be
// found in the LICENSE file.

package xmpp

import (
	"encoding/xml"
	"strings"

	"golang.org/x/text/language"
	"mellium.im/xmpp/jid"
	"mellium.im/xmpp/ns"
)

type errorType int

const (
	// An error with type Cancel indicates that the error cannot be remedied and
	// the operation should not be retried.
	Cancel errorType = iota

	// An error with type Auth indicates that an operation should be retried after
	// providing credentials.
	Auth

	// An error with type Continue indicates that the operation can proceed (the
	// condition was only a warning).
	Continue

	// An error with type Modify indicates that the operation can be retried after
	// changing the data sent.
	Modify

	// An error with type Wait is temporary and may be retried after waiting.
	Wait
)

func (t errorType) MarshalXMLAttr(name xml.Name) (xml.Attr, error) {
	return xml.Attr{Name: name, Value: strings.ToLower(t.String())}, nil
}

func (t *errorType) UnmarshalXMLAttr(attr xml.Attr) error {
	switch attr.Value {
	case "auth":
		*t = Auth
	case "continue":
		*t = Continue
	case "modify":
		*t = Modify
	case "wait":
		*t = Wait
	default: // case "cancel":
		*t = Cancel
	}
	return nil
}

// condition represents a stanza error condition that can be encapsulated by an
// <error/> element.
type condition string

// A list of stanza error conditions defined in RFC 6120 §8.3.3
const (
	BadRequest            condition = "bad-request"
	Conflict              condition = "conflict"
	FeatureNotImplemented condition = "feature-not-implemented"
	Forbidden             condition = "forbidden"
	Gone                  condition = "gone"
	InternalServerError   condition = "internal-server-error"
	ItemNotFound          condition = "item-not-found"
	JIDMalformed          condition = "jid-malformed"
	NotAcceptable         condition = "not-acceptable"
	NotAllowed            condition = "not-allowed"
	NotAuthorized         condition = "not-authorized"
	PolicyViolation       condition = "policy-violation"
	RecipientUnavailable  condition = "recipient-unavailable"
	Redirect              condition = "redirect"
	RegistrationRequired  condition = "registration-required"
	RemoteServerNotFound  condition = "remote-server-not-found"
	RemoteServerTimeout   condition = "remote-server-timeout"
	ResourceConstraint    condition = "resource-constraint"
	ServiceUnavailable    condition = "service-unavailable"
	SubscriptionRequired  condition = "subscription-required"
	UndefinedCondition    condition = "undefined-condition"
	UnexpectedRequest     condition = "unexpected-request"
)

// StanzaError is an implementation of error intended to be marshalable and
// unmarshalable as XML.
type StanzaError struct {
	XMLName   xml.Name
	By        *jid.JID
	Type      errorType
	Condition condition
	Lang      language.Tag
	Text      string
}

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

func (se StanzaError) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error) {
	start = xml.StartElement{
		Name: xml.Name{Space: ``, Local: "error"},
		Attr: []xml.Attr{},
	}
	typattr, _ := se.Type.MarshalXMLAttr(xml.Name{Space: "", Local: "type"})
	start.Attr = append(start.Attr, typattr)
	if se.By != nil {
		a, _ := se.By.MarshalXMLAttr(xml.Name{Space: "", Local: "by"})
		start.Attr = append(start.Attr, a)
	}
	if err = e.EncodeToken(start); err != nil {
		return err
	}
	condition := xml.StartElement{
		Name: xml.Name{Space: ns.Stanza, Local: string(se.Condition)},
	}
	if err = e.EncodeToken(condition); err != nil {
		return err
	}
	if err = e.EncodeToken(condition.End()); err != nil {
		return err
	}
	if se.Text != "" {
		text := xml.StartElement{
			Name: xml.Name{Space: ns.Stanza, Local: "text"},
			Attr: []xml.Attr{
				xml.Attr{
					Name:  xml.Name{Space: ns.XML, Local: "lang"},
					Value: se.Lang.String(),
				},
			},
		}
		if err = e.EncodeToken(text); err != nil {
			return err
		}
		if err = e.EncodeToken(xml.CharData(se.Text)); err != nil {
			return err
		}
		if err = e.EncodeToken(text.End()); err != nil {
			return err
		}
	}
	e.EncodeToken(start.End())
	return nil
}

func (se *StanzaError) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
	decoded := struct {
		Condition struct {
			XMLName xml.Name
		} `xml:",any"`
		Type errorType `xml:"type,attr"`
		By   *jid.JID  `xml:"by,attr"`
		Text []struct {
			Lang string `xml:"http://www.w3.org/XML/1998/namespace lang,attr"`
			Data string `xml:",chardata"`
		} `xml:"urn:ietf:params:xml:ns:xmpp-stanzas text"`
	}{}
	if err := d.DecodeElement(&decoded, &start); err != nil {
		return err
	}
	se.Type = decoded.Type
	se.By = decoded.By
	// TODO: Oh god why… maybe I should transform the String() output instead.
	switch decoded.Condition.XMLName.Local {
	case "bad-request":
		se.Condition = BadRequest
	case "conflict":
		se.Condition = Conflict
	case "feature-not-implemented":
		se.Condition = FeatureNotImplemented
	case "forbidden":
		se.Condition = Forbidden
	case "gone":
		se.Condition = Gone
	case "internal-server-error":
		se.Condition = InternalServerError
	case "item-not-found":
		se.Condition = ItemNotFound
	case "jid-malformed":
		se.Condition = JIDMalformed
	case "not-acceptable":
		se.Condition = NotAcceptable
	case "not-allowed":
		se.Condition = NotAllowed
	case "not-authorized":
		se.Condition = NotAuthorized
	case "policy-violation":
		se.Condition = PolicyViolation
	case "recipient-unavailable":
		se.Condition = RecipientUnavailable
	case "redirect":
		se.Condition = Redirect
	case "registration-required":
		se.Condition = RegistrationRequired
	case "remote-server-not-found":
		se.Condition = RemoteServerNotFound
	case "remote-server-timeout":
		se.Condition = RemoteServerTimeout
	case "resource-constraint":
		se.Condition = ResourceConstraint
	case "service-unavailable":
		se.Condition = ServiceUnavailable
	case "subscription-required":
		se.Condition = SubscriptionRequired
	case "undefined-condition":
		se.Condition = UndefinedCondition
	case "unexpected-request":
		se.Condition = UnexpectedRequest
	default:
		if decoded.Condition.XMLName.Space == ns.Stanza {
			se.Condition = condition(decoded.Condition.XMLName.Local)
		}
	}

	// TODO: Dedup this (and probably a lot of other stuff) with the saslerr
	// logic.
	tags := make([]language.Tag, 0, len(decoded.Text))
	data := make(map[language.Tag]string)
	for _, text := range decoded.Text {
		tag, err := language.Parse(text.Lang)
		if err != nil {
			continue
		}
		tags = append(tags, tag)
		data[tag] = text.Data
	}
	tag, _, _ := language.NewMatcher(tags).Match(se.Lang)
	se.Lang = tag
	se.Text, _ = data[tag]
	return nil
}