# HG changeset patch # User Chris Jones # Date 1325182672 25200 # Node ID 1dc47df5c99fe1b1143579974c79261570eb01f0 # Parent a77fc342e013be61c15467acfe75ec2661c7f798 Made streamError non-public, and made a first attempt at a stream error handler. diff -r a77fc342e013 -r 1dc47df5c99f stream.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, diff -r a77fc342e013 -r 1dc47df5c99f structs.go --- 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{} // -// 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("") xml.Marshal(buf, s.Any) diff -r a77fc342e013 -r 1dc47df5c99f structs_test.go --- 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 := ``; 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 = `things happen` diff -r a77fc342e013 -r 1dc47df5c99f xmpp_test.go --- 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 := `` 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)