diff options
Diffstat (limited to 'vendor')
-rw-r--r-- | vendor/github.com/matterbridge/gozulipbot/queue.go | 57 | ||||
-rw-r--r-- | vendor/modules.txt | 2 |
2 files changed, 48 insertions, 11 deletions
diff --git a/vendor/github.com/matterbridge/gozulipbot/queue.go b/vendor/github.com/matterbridge/gozulipbot/queue.go index 9a37a8e7..d6e910ee 100644 --- a/vendor/github.com/matterbridge/gozulipbot/queue.go +++ b/vendor/github.com/matterbridge/gozulipbot/queue.go @@ -1,6 +1,7 @@ package gozulipbot import ( + "bytes" "encoding/json" "fmt" "io/ioutil" @@ -13,10 +14,12 @@ import ( ) var ( - HeartbeatError = fmt.Errorf("EventMessage is a heartbeat") - UnauthorizedError = fmt.Errorf("Request is unauthorized") - BackoffError = fmt.Errorf("Too many requests") - UnknownError = fmt.Errorf("Error was unknown") + HeartbeatError = fmt.Errorf("EventMessage is a heartbeat") + UnauthorizedError = fmt.Errorf("Request is unauthorized") + BackoffError = fmt.Errorf("Too many requests") + BadEventQueueError = fmt.Errorf("BAD_EVENT_QUEUE_ID error") + UnknownError = fmt.Errorf("Error was unknown") + NoJSONError = fmt.Errorf("No JSON in body found") ) type Queue struct { @@ -26,6 +29,13 @@ type Queue struct { Bot *Bot `json:"-"` } +type QueueError struct { + Code string `json:"code"` + Msg string `json:"msg"` + ID string `json:"queue_id"` + Result string `json:"result"` +} + func (q *Queue) EventsChan() (chan EventMessage, func()) { end := false endFunc := func() { @@ -131,18 +141,25 @@ func (q *Queue) GetEvents() ([]EventMessage, error) { } defer resp.Body.Close() + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return nil, err + } + switch { case resp.StatusCode == 429: return nil, BackoffError case resp.StatusCode == 403: return nil, UnauthorizedError case resp.StatusCode >= 400: - return nil, UnknownError - } - - body, err := ioutil.ReadAll(resp.Body) - if err != nil { - return nil, err + if bytes.HasPrefix(body, []byte("<")) { + return nil, NoJSONError + } + qErr, err := q.ParseError(body) + if err != nil || qErr == nil { + return nil, UnknownError + } + return nil, BadEventQueueError } msgs, err := q.ParseEventMessages(body) @@ -170,6 +187,26 @@ func (q *Queue) RawGetEvents() (*http.Response, error) { return q.Bot.Client.Do(req) } +func (q *Queue) ParseError(rawEventResponse []byte) (*QueueError, error) { + rawResponse := map[string]json.RawMessage{} + err := json.Unmarshal(rawEventResponse, &rawResponse) + if err != nil { + return nil, err + } + + if _, ok := rawResponse["code"]; ok { + var qErr QueueError + err = json.Unmarshal(rawEventResponse, &qErr) + if err != nil { + return nil, err + } + if qErr.Code == "BAD_EVENT_QUEUE_ID" { + return &qErr, nil + } + } + return nil, nil +} + func (q *Queue) ParseEventMessages(rawEventResponse []byte) ([]EventMessage, error) { rawResponse := map[string]json.RawMessage{} err := json.Unmarshal(rawEventResponse, &rawResponse) diff --git a/vendor/modules.txt b/vendor/modules.txt index a2e4a3da..8c8160b6 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -78,7 +78,7 @@ github.com/matterbridge/Rocket.Chat.Go.SDK/rest github.com/matterbridge/go-xmpp # github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea github.com/matterbridge/gomatrix -# github.com/matterbridge/gozulipbot v0.0.0-20180507190239-b6bb12d33544 +# github.com/matterbridge/gozulipbot v0.0.0-20190212232658-7aa251978a18 github.com/matterbridge/gozulipbot # github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61 github.com/matterbridge/logrus-prefixed-formatter |