Skip to content

Commit

Permalink
errors: update and implement flood error
Browse files Browse the repository at this point in the history
  • Loading branch information
demget committed Nov 3, 2020
1 parent ad12779 commit 315d880
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 6 deletions.
15 changes: 13 additions & 2 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ type APIError struct {
Message string
}

type FloodError struct {
*APIError
RetryAfter int
}

// ʔ returns description of error.
// A tiny shortcut to make code clearer.
func (err *APIError) ʔ() string {
Expand Down Expand Up @@ -45,7 +50,7 @@ func NewAPIError(code int, msgs ...string) *APIError {
return err
}

var errorRx = regexp.MustCompile(`{.+"error_code":(\d+),"description":"(.+)".*}`)
var errorRx = regexp.MustCompile(`{.+"error_code":(\d+),"description":"(.+)"(?:,"parameters":{"retry_after":(\d+)})?}`)

var (
// General errors
Expand All @@ -67,6 +72,8 @@ var (
ErrEmptyChatID = NewAPIError(400, "Bad Request: chat_id is empty")
ErrChatNotFound = NewAPIError(400, "Bad Request: chat not found")
ErrMessageNotModified = NewAPIError(400, "Bad Request: message is not modified")
ErrSameMessageContent = NewAPIError(400, "Bad Request: message is not modified: specified new message content and reply markup are exactly the same as a current content and reply markup of the message")
ErrCantEditMessage = NewAPIError(400, "Bad Request: message can't be edited")
ErrButtonDataInvalid = NewAPIError(400, "Bad Request: BUTTON_DATA_INVALID")
ErrWrongTypeOfContent = NewAPIError(400, "Bad Request: wrong type of the web page content")
ErrBadURLContent = NewAPIError(400, "Bad Request: failed to get HTTP URL content")
Expand All @@ -77,7 +84,7 @@ var (
ErrWrongFileIDPadding = NewAPIError(400, "Bad Request: wrong remote file id specified: Wrong padding in the string")
ErrFailedImageProcess = NewAPIError(400, "Bad Request: IMAGE_PROCESS_FAILED", "Image process failed")
ErrInvalidStickerSet = NewAPIError(400, "Bad Request: STICKERSET_INVALID", "Stickerset is invalid")
ErrBadPollOptions = NewAPIError(400, "Bad Request: expected Array of String as options")
ErrBadPollOptions = NewAPIError(400, "Bad Request: expected an Array of String as options")

// No rights errors
ErrNoRightsToRestrict = NewAPIError(400, "Bad Request: not enough rights to restrict/unrestrict chat member")
Expand Down Expand Up @@ -124,6 +131,10 @@ func ErrByDescription(s string) error {
return ErrChatNotFound
case ErrMessageNotModified.ʔ():
return ErrMessageNotModified
case ErrSameMessageContent.ʔ():
return ErrSameMessageContent
case ErrCantEditMessage.ʔ():
return ErrCantEditMessage
case ErrButtonDataInvalid.ʔ():
return ErrButtonDataInvalid
case ErrBadPollOptions.ʔ():
Expand Down
1 change: 1 addition & 0 deletions sendable.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ func (p *Poll) Send(b *Bot, to Recipient, opt *SendOptions) (*Message, error) {
for _, o := range p.Options {
options = append(options, o.Text)
}

opts, _ := json.Marshal(options)
params["options"] = string(opts)

Expand Down
15 changes: 14 additions & 1 deletion util.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"

"github.com/pkg/errors"
Expand Down Expand Up @@ -56,10 +57,22 @@ func extractOk(data []byte) error {

desc := match[2]
err := ErrByDescription(desc)

if err == nil {
code, _ := strconv.Atoi(match[1])
err = fmt.Errorf("telegram unknown: %s (%d)", desc, code)

switch code {
case http.StatusTooManyRequests:
retry, _ := strconv.Atoi(match[3])
err = FloodError{
APIError: NewAPIError(429, desc),
RetryAfter: retry,
}
default:
err = fmt.Errorf("telegram unknown: %s (%d)", desc, code)
}
}

return err
}

Expand Down
9 changes: 6 additions & 3 deletions util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ func TestExtractOk(t *testing.T) {
data := []byte(`{"ok":true,"result":{"foo":"bar"}}`)
require.NoError(t, extractOk(data))

data = []byte(`{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 8","parameters":{"retry_after":8}}`)
assert.Error(t, extractOk(data))

data = []byte(`{"ok":false,"error_code":400,"description":"Bad Request: reply message not found"}`)
assert.EqualError(t, extractOk(data), ErrToReplyNotFound.Error())

data = []byte(`{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 8","parameters":{"retry_after":8}}`)
assert.Equal(t, FloodError{
APIError: NewAPIError(429, "Too Many Requests: retry after 8"),
RetryAfter: 8,
}, extractOk(data))
}

func TestExtractMessage(t *testing.T) {
Expand Down

0 comments on commit 315d880

Please sign in to comment.