Made streamError non-public, and made a first attempt at a stream
authorChris Jones <chris@cjones.org>
Thu, 29 Dec 2011 11:17:52 -0700
changeset 31 1dc47df5c99f
parent 30 a77fc342e013
child 32 4e68d8f89dc3
Made streamError non-public, and made a first attempt at a stream error handler.
stream.go
structs.go
structs_test.go
xmpp_test.go
--- a/stream.go	Thu Dec 29 11:11:18 2011 -0700
+++ b/stream.go	Thu Dec 29 11:17:52 2011 -0700
@@ -117,7 +117,7 @@
 			ch <- st
 			continue
 		case "stream error", nsStream + " error":
-			obj = &StreamError{}
+			obj = &streamError{}
 		case nsStream + " features":
 			obj = &Features{}
 		case nsTLS + " proceed", nsTLS + " failure":
@@ -186,6 +186,8 @@
 			switch obj := x.(type) {
 			case *stream:
 				handleStream(obj)
+			case *streamError:
+				cl.handleStreamError(obj)
 			case *Features:
 				cl.handleFeatures(obj)
 			case *starttls:
@@ -245,6 +247,11 @@
 func handleStream(ss *stream) {
 }
 
+func (cl *Client) handleStreamError(se *streamError) {
+	log.Printf("Received stream error: %v", se)
+	cl.Close()
+}
+
 func (cl *Client) handleFeatures(fe *Features) {
 	if fe.Starttls != nil {
 		start := &starttls{XMLName: xml.Name{Space: nsTLS,
--- a/structs.go	Thu Dec 29 11:11:18 2011 -0700
+++ b/structs.go	Thu Dec 29 11:17:52 2011 -0700
@@ -40,12 +40,11 @@
 var _ fmt.Stringer = &stream{}
 
 // <stream:error>
-// BUG(cjyar) Can this be consolidated with Error? Hide it if not.
-type StreamError struct {
+type streamError struct {
 	Any Generic
 	Text *errText
 }
-var _ xml.Marshaler = &StreamError{}
+var _ xml.Marshaler = &streamError{}
 
 type errText struct {
 	Lang string `xml:"attr"`
@@ -222,7 +221,7 @@
 	return s, nil
 }
 
-func (s *StreamError) MarshalXML() ([]byte, os.Error) {
+func (s *streamError) MarshalXML() ([]byte, os.Error) {
 	buf := bytes.NewBuffer(nil)
 	buf.WriteString("<stream:error>")
 	xml.Marshal(buf, s.Any)
--- a/structs_test.go	Thu Dec 29 11:11:18 2011 -0700
+++ b/structs_test.go	Thu Dec 29 11:17:52 2011 -0700
@@ -69,13 +69,13 @@
 
 func TestStreamErrorMarshal(t *testing.T) {
 	name := xml.Name{Space: nsStreams, Local: "ack"}
-	e := &StreamError{Any: Generic{XMLName: name}}
+	e := &streamError{Any: Generic{XMLName: name}}
 	exp := `<stream:error><ack xmlns="` + nsStreams +
 		`"></ack></stream:error>`;
 	assertMarshal(t, exp, e)
 
 	txt := errText{Lang: "pt", Text: "things happen"}
-	e = &StreamError{Any: Generic{XMLName: name}, Text: &txt}
+	e = &streamError{Any: Generic{XMLName: name}, Text: &txt}
 	exp = `<stream:error><ack xmlns="` + nsStreams +
 		`"></ack><text xmlns="` + nsStreams +
 		`" xml:lang="pt">things happen</text></stream:error>`
--- a/xmpp_test.go	Thu Dec 29 11:11:18 2011 -0700
+++ b/xmpp_test.go	Thu Dec 29 11:17:52 2011 -0700
@@ -18,7 +18,7 @@
 	ch := make(chan interface{})
 	go readXml(r, ch)
 	x := <- ch
-	se, ok := x.(*StreamError)
+	se, ok := x.(*streamError)
 	if !ok {
 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
 	}
@@ -34,7 +34,7 @@
 	ch = make(chan interface{})
 	go readXml(r, ch)
 	x = <- ch
-	se, ok = x.(*StreamError)
+	se, ok = x.(*streamError)
 	if !ok {
 		t.Fatalf("not StreamError: %v", reflect.TypeOf(x))
 	}
@@ -78,13 +78,13 @@
 }
 
 func TestWriteError(t *testing.T) {
-	se := &StreamError{Any: Generic{XMLName: xml.Name{Local:
+	se := &streamError{Any: Generic{XMLName: xml.Name{Local:
 				"blah"}}}
 	str := testWrite(se)
 	exp := `<stream:error><blah></blah></stream:error>`
 	assertEquals(t, exp, str)
 
-	se = &StreamError{Any: Generic{XMLName: xml.Name{Space:
+	se = &streamError{Any: Generic{XMLName: xml.Name{Space:
 				nsStreams, Local: "foo"}}, Text:
 		&errText{Lang: "ru", Text: "Пошёл ты"}}
 	str = testWrite(se)