Fix error handling on bad event queue id (zulip). Closes #694

This commit is contained in:
Wim 2019-02-11 01:34:50 +01:00
parent 8147815037
commit 40d76b2296
5 changed files with 63 additions and 15 deletions

View File

@ -100,7 +100,23 @@ func (b *Bzulip) getChannel(id int) string {
func (b *Bzulip) handleQueue() error { func (b *Bzulip) handleQueue() error {
for { for {
messages, _ := b.q.GetEvents() messages, err := b.q.GetEvents()
switch err {
case gzb.BackoffError:
time.Sleep(time.Second * 5)
case gzb.BadEventQueueError:
b.Log.Info("got a bad event queue id error, reconnecting")
b.bot.Queues = nil
b.q, err = b.bot.RegisterAll()
if err != nil {
b.Log.Errorf("reconnecting failed: %s. Sleeping 10 seconds", err)
time.Sleep(time.Second * 10)
continue
}
continue
default:
b.Log.Debugf("receiving error: %#v", err)
}
for _, m := range messages { for _, m := range messages {
b.Log.Debugf("== Receiving %#v", m) b.Log.Debugf("== Receiving %#v", m)
// ignore our own messages // ignore our own messages

2
go.mod
View File

@ -25,7 +25,7 @@ require (
github.com/lusis/slack-test v0.0.0-20180109053238-3c758769bfa6 // indirect github.com/lusis/slack-test v0.0.0-20180109053238-3c758769bfa6 // indirect
github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91 github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91
github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea
github.com/matterbridge/gozulipbot v0.0.0-20180507190239-b6bb12d33544 github.com/matterbridge/gozulipbot v0.0.0-20190211002717-befe50663a92
github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61 github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61
github.com/mattermost/mattermost-server v5.5.0+incompatible github.com/mattermost/mattermost-server v5.5.0+incompatible
github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect github.com/mgutz/ansi v0.0.0-20170206155736-9520e82c474b // indirect

4
go.sum
View File

@ -70,8 +70,8 @@ github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91 h1:KzDEcy8eDb
github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91/go.mod h1:ECDRehsR9TYTKCAsRS8/wLeOk6UUqDydw47ln7wG41Q= github.com/matterbridge/go-xmpp v0.0.0-20180529212104-cd19799fba91/go.mod h1:ECDRehsR9TYTKCAsRS8/wLeOk6UUqDydw47ln7wG41Q=
github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea h1:kaADGqpK4gGO2BpzEyJrBxq2Jc57Rsar4i2EUxcACUc= github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea h1:kaADGqpK4gGO2BpzEyJrBxq2Jc57Rsar4i2EUxcACUc=
github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea/go.mod h1:+jWeaaUtXQbBRdKYWfjW6JDDYiI2XXE+3NnTjW5kg8g= github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea/go.mod h1:+jWeaaUtXQbBRdKYWfjW6JDDYiI2XXE+3NnTjW5kg8g=
github.com/matterbridge/gozulipbot v0.0.0-20180507190239-b6bb12d33544 h1:A8lLG3DAu75B5jITHs9z4JBmU6oCq1WiUNnDAmqKCZc= github.com/matterbridge/gozulipbot v0.0.0-20190211002717-befe50663a92 h1:4L4vltV2H74Wl+fuwH3xDBe5jvFTn85UXdTqkCPs6Mg=
github.com/matterbridge/gozulipbot v0.0.0-20180507190239-b6bb12d33544/go.mod h1:yAjnZ34DuDyPHMPHHjOsTk/FefW4JJjoMMCGt/8uuQA= github.com/matterbridge/gozulipbot v0.0.0-20190211002717-befe50663a92/go.mod h1:yAjnZ34DuDyPHMPHHjOsTk/FefW4JJjoMMCGt/8uuQA=
github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61 h1:R/MgM/eUyRBQx2FiH6JVmXck8PaAuKfe2M1tWIzW7nE= github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61 h1:R/MgM/eUyRBQx2FiH6JVmXck8PaAuKfe2M1tWIzW7nE=
github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61/go.mod h1:iXGEotOvwI1R1SjLxRc+BF5rUORTMtE0iMZBT2lxqAU= github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61/go.mod h1:iXGEotOvwI1R1SjLxRc+BF5rUORTMtE0iMZBT2lxqAU=
github.com/mattermost/mattermost-server v5.5.0+incompatible h1:0wcLGgYtd+YImtLDPf2AOfpBHxbU4suATx+6XKw1XbU= github.com/mattermost/mattermost-server v5.5.0+incompatible h1:0wcLGgYtd+YImtLDPf2AOfpBHxbU4suATx+6XKw1XbU=

View File

@ -16,6 +16,7 @@ var (
HeartbeatError = fmt.Errorf("EventMessage is a heartbeat") HeartbeatError = fmt.Errorf("EventMessage is a heartbeat")
UnauthorizedError = fmt.Errorf("Request is unauthorized") UnauthorizedError = fmt.Errorf("Request is unauthorized")
BackoffError = fmt.Errorf("Too many requests") BackoffError = fmt.Errorf("Too many requests")
BadEventQueueError = fmt.Errorf("BAD_EVENT_QUEUE_ID error")
UnknownError = fmt.Errorf("Error was unknown") UnknownError = fmt.Errorf("Error was unknown")
) )
@ -26,6 +27,13 @@ type Queue struct {
Bot *Bot `json:"-"` 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()) { func (q *Queue) EventsChan() (chan EventMessage, func()) {
end := false end := false
endFunc := func() { endFunc := func() {
@ -131,18 +139,22 @@ func (q *Queue) GetEvents() ([]EventMessage, error) {
} }
defer resp.Body.Close() defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
switch { switch {
case resp.StatusCode == 429: case resp.StatusCode == 429:
return nil, BackoffError return nil, BackoffError
case resp.StatusCode == 403: case resp.StatusCode == 403:
return nil, UnauthorizedError return nil, UnauthorizedError
case resp.StatusCode >= 400: case resp.StatusCode >= 400:
qErr, err := q.ParseError(body)
if err != nil || qErr == nil {
return nil, UnknownError return nil, UnknownError
} }
return nil, BadEventQueueError
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
} }
msgs, err := q.ParseEventMessages(body) msgs, err := q.ParseEventMessages(body)
@ -170,6 +182,26 @@ func (q *Queue) RawGetEvents() (*http.Response, error) {
return q.Bot.Client.Do(req) 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) { func (q *Queue) ParseEventMessages(rawEventResponse []byte) ([]EventMessage, error) {
rawResponse := map[string]json.RawMessage{} rawResponse := map[string]json.RawMessage{}
err := json.Unmarshal(rawEventResponse, &rawResponse) err := json.Unmarshal(rawEventResponse, &rawResponse)

2
vendor/modules.txt vendored
View File

@ -70,7 +70,7 @@ github.com/magiconair/properties
github.com/matterbridge/go-xmpp github.com/matterbridge/go-xmpp
# github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea # github.com/matterbridge/gomatrix v0.0.0-20190102230110-6f9631ca6dea
github.com/matterbridge/gomatrix github.com/matterbridge/gomatrix
# github.com/matterbridge/gozulipbot v0.0.0-20180507190239-b6bb12d33544 # github.com/matterbridge/gozulipbot v0.0.0-20190211002717-befe50663a92
github.com/matterbridge/gozulipbot github.com/matterbridge/gozulipbot
# github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61 # github.com/matterbridge/logrus-prefixed-formatter v0.0.0-20180806162718-01618749af61
github.com/matterbridge/logrus-prefixed-formatter github.com/matterbridge/logrus-prefixed-formatter