errors: update and implement flood error

pull/341/head
Demian 4 years ago
parent 8fe81bb3bf
commit e467d6286c

@ -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 {
@ -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
@ -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")
@ -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")
@ -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.ʔ():

@ -368,6 +368,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)

@ -2,7 +2,9 @@ package telebot
import (
"encoding/json"
"fmt"
"log"
"net/http"
"strconv"
"github.com/pkg/errors"
@ -64,10 +66,22 @@ func extractOk(data []byte) error {
desc := match[2]
err := ErrByDescription(desc)
if err == nil {
code, _ := strconv.Atoi(match[1])
err = errors.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
}

@ -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) {

Loading…
Cancel
Save