-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherror.go
More file actions
159 lines (146 loc) · 3.27 KB
/
error.go
File metadata and controls
159 lines (146 loc) · 3.27 KB
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
package xmpp
import (
"encoding/xml"
"errors"
)
// Errors are a little tricky to unmarshal, because of their irregular structure:
//
// <stream:error>
// <defined-condition xmlns='urn:ietf:params:xml:ns:xmpp-streams'/>
// [<text xmlns='urn:ietf:params:xml:ns:xmpp-streams'
// xml:lang='langcode'>
// OPTIONAL descriptive text
// </text>]
// [OPTIONAL application-specific condition element]
// </stream:error>
//
// This implementation doesn't handle <text> and custom elements yet
// DefinedConditions is a list of conditions defined by RFC6120
var DefinedConditions = []string{
"bad-format",
"bad-namespace-prefix",
"conflict",
"connection-timeout",
"host-gone",
"host-unknown",
"improper-addressing",
"internal-server-error",
"invalid-from",
"invalid-namespace",
"invalid-xml",
"not-authorized",
"not-well-formed",
"policy-violation",
"remote-connection-failed",
"reset",
"resource-constraint",
"restricted-xml",
"see-other-host",
"system-shutdown",
"undefined-condition",
"unsupported-encoding",
"unsupported-feature",
"unsupported-stanza-type",
"unsupported-version",
}
type Error struct {
Condition string
Text string
Extra interface{}
}
func (e *Error) UnmarshalXML(dec *xml.Decoder, start xml.StartElement) error {
// First element should be the condition
token, err := dec.Token()
if err != nil {
return err
}
start, ok := token.(xml.StartElement)
if !ok {
return errors.New("failed to parse error: unexpected element")
}
e.Condition = start.Name.Local
if err := dec.Skip(); err != nil {
return err
}
// Get the optional text element
token, err = dec.Token()
if err != nil {
return err
}
start, ok = token.(xml.StartElement)
if !ok {
return errors.New("failed to parse error: unexpected element")
}
if start.Name.Local == "text" {
textElem := &struct {
Text string `xml:",chardata"`
}{}
if err := dec.DecodeElement(textElem, &start); err != nil {
return err
}
e.Text = textElem.Text
} else {
p := &proxy{}
if err := dec.DecodeElement(p, &start); err != nil {
return err
}
e.Extra = p.Object
return dec.Skip()
}
// Get the optional application specific element
token, err = dec.Token()
if err != nil {
return err
}
if _, ok := token.(xml.EndElement); ok {
return nil
}
start, ok = token.(xml.StartElement)
if !ok {
return errors.New("failed to parse error: unexpected element")
}
p := &proxy{}
if err := dec.DecodeElement(p, &start); err != nil {
return err
}
e.Extra = p.Object
return dec.Skip()
}
func (e *Error) MarshalXML(enc *xml.Encoder, start xml.StartElement) error {
start.Name.Local = "error"
if err := enc.EncodeToken(start); err != nil {
return err
}
// Write error
condition := &struct {
XMLName xml.Name
}{
XMLName: xml.Name{
Space: nsStreams,
Local: e.Condition,
},
}
if err := enc.Encode(condition); err != nil {
return err
}
// Write text
if e.Text != "" {
text := &struct {
XMLName xml.Name `xml:"urn:ietf:params:xml:ns:xmpp-streams text"`
Text string `xml:",chardata"`
}{
Text: e.Text,
}
if err := enc.Encode(text); err != nil {
return err
}
}
// Write extra element
if e.Extra != nil {
return enc.Encode(e.Extra)
}
return enc.EncodeToken(start.End())
}
func addErrorElements() {
AddElement(&Error{})
}