diff options
author | Wim <wim@42.be> | 2019-02-11 01:34:50 +0100 |
---|---|---|
committer | Wim <wim@42.be> | 2019-02-11 01:34:50 +0100 |
commit | 40d76b22960fcc57e167fb08b97f4ff3aec31090 (patch) | |
tree | 832a9b0cb7aa02df10083e18be91a98434e227a5 /vendor/github.com/matterbridge/gozulipbot/queue.go | |
parent | 81478150373979a3061d94948ad2f2842ee87243 (diff) | |
download | matterbridge-msglm-40d76b22960fcc57e167fb08b97f4ff3aec31090.tar.gz matterbridge-msglm-40d76b22960fcc57e167fb08b97f4ff3aec31090.tar.bz2 matterbridge-msglm-40d76b22960fcc57e167fb08b97f4ff3aec31090.zip |
Fix error handling on bad event queue id (zulip). Closes #694
Diffstat (limited to 'vendor/github.com/matterbridge/gozulipbot/queue.go')
-rw-r--r-- | vendor/github.com/matterbridge/gozulipbot/queue.go | 52 |
1 files changed, 42 insertions, 10 deletions
diff --git a/vendor/github.com/matterbridge/gozulipbot/queue.go b/vendor/github.com/matterbridge/gozulipbot/queue.go index 9a37a8e7..af32bb6e 100644 --- a/vendor/github.com/matterbridge/gozulipbot/queue.go +++ b/vendor/github.com/matterbridge/gozulipbot/queue.go @@ -13,10 +13,11 @@ 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") ) type Queue struct { @@ -26,6 +27,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 +139,22 @@ 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 + qErr, err := q.ParseError(body) + if err != nil || qErr == nil { + return nil, UnknownError + } + return nil, BadEventQueueError } msgs, err := q.ParseEventMessages(body) @@ -170,6 +182,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) |