From 52ef3ffa80c032c4fea94379d9d11089f1b2601c Mon Sep 17 00:00:00 2001 From: Alexander Fallenstedt Date: Fri, 8 Jan 2021 23:14:11 -0800 Subject: [PATCH] have start stream return err (#10) --- stream.go | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/stream.go b/stream.go index e8341ed..141ba29 100644 --- a/stream.go +++ b/stream.go @@ -8,7 +8,7 @@ import ( type ( // IStream is the interface that the stream struct implements. IStream interface { - StartStream() + StartStream() error StopStream() GetMessages() *chan interface{} } @@ -43,7 +43,7 @@ func (s *stream) StopStream() { } // StartStream makes an HTTP request to twitter and starts streaming tweets to the Messages channel. -func (s *stream) StartStream() { +func (s *stream) StartStream() error { res, err := s.httpClient.newHttpRequest(&requestOpts{ Method: "GET", @@ -51,13 +51,14 @@ func (s *stream) StartStream() { }) if err != nil { - panic(err) + return err } s.reader.setStreamResponseBody(res.Body) s.group.Add(1) go s.streamMessages(res) + return nil } func (s *stream) streamMessages(res *http.Response) { @@ -67,7 +68,9 @@ func (s *stream) streamMessages(res *http.Response) { for !stopped(s.done) { data, err := s.reader.readNext() if err != nil { - return + s.messages <- err + s.StopStream() + break } if len(data) == 0 { // empty keep-alive @@ -75,7 +78,6 @@ func (s *stream) streamMessages(res *http.Response) { } m := string(data) - // TODO send data or error here s.messages <- m } }