telebot/errors.go

184 lines
7.0 KiB
Go
Raw Normal View History

package telebot
import (
"fmt"
"regexp"
"strings"
)
2020-04-05 17:23:51 +00:00
type APIError struct {
2020-03-27 23:05:46 +00:00
Code int
Description string
2020-04-05 17:23:51 +00:00
Message string
}
type FloodError struct {
*APIError
RetryAfter int
}
2020-04-05 17:23:51 +00:00
// ʔ returns description of error.
2020-05-13 01:33:12 +00:00
// A tiny shortcut to make code clearer.
2020-04-05 17:23:51 +00:00
func (err *APIError) ʔ() string {
return err.Description
}
2020-04-05 17:23:51 +00:00
// Error implements error interface.
func (err *APIError) Error() string {
msg := err.Message
if msg == "" {
split := strings.Split(err.Description, ": ")
if len(split) == 2 {
msg = split[1]
2020-03-27 23:05:46 +00:00
} else {
2020-04-05 17:23:51 +00:00
msg = err.Description
}
}
2020-04-05 18:29:05 +00:00
return fmt.Sprintf("telegram: %s (%d)", msg, err.Code)
}
2020-04-05 17:23:51 +00:00
// NewAPIError returns new APIError instance with given description.
// First element of msgs is Description. The second is optional Message.
func NewAPIError(code int, msgs ...string) *APIError {
err := &APIError{Code: code}
if len(msgs) >= 1 {
err.Description = msgs[0]
}
if len(msgs) >= 2 {
err.Message = msgs[1]
}
return err
}
var errorRx = regexp.MustCompile(`{.+"error_code":(\d+),"description":"(.+)"(?:,"parameters":{"retry_after":(\d+)})?}`)
2020-04-05 17:23:51 +00:00
var (
2020-04-05 18:29:05 +00:00
// General errors
ErrUnauthorized = NewAPIError(401, "Unauthorized")
2020-04-27 21:16:21 +00:00
ErrNotStartedByUser = NewAPIError(403, "Forbidden: bot can't initiate conversation with a user")
2020-04-05 19:09:40 +00:00
ErrBlockedByUser = NewAPIError(401, "Forbidden: bot was blocked by the user")
2020-04-05 18:29:05 +00:00
ErrUserIsDeactivated = NewAPIError(401, "Forbidden: user is deactivated")
ErrNotFound = NewAPIError(404, "Not Found")
2020-04-27 18:13:09 +00:00
ErrInternal = NewAPIError(500, "Internal Server Error")
2020-04-05 17:23:51 +00:00
// Bad request errors
2020-04-05 19:09:40 +00:00
ErrTooLarge = NewAPIError(400, "Request Entity Too Large")
ErrMessageTooLong = NewAPIError(400, "Bad Request: message is too long")
ErrToForwardNotFound = NewAPIError(400, "Bad Request: message to forward not found")
ErrToReplyNotFound = NewAPIError(400, "Bad Request: reply message not found")
ErrToDeleteNotFound = NewAPIError(400, "Bad Request: message to delete not found")
ErrEmptyMessage = NewAPIError(400, "Bad Request: message must be non-empty")
ErrEmptyText = NewAPIError(400, "Bad Request: text is empty")
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")
2020-04-23 18:55:52 +00:00
ErrButtonDataInvalid = NewAPIError(400, "Bad Request: BUTTON_DATA_INVALID")
2020-04-05 19:09:40 +00:00
ErrWrongTypeOfContent = NewAPIError(400, "Bad Request: wrong type of the web page content")
ErrBadURLContent = NewAPIError(400, "Bad Request: failed to get HTTP URL content")
ErrWrongFileID = NewAPIError(400, "Bad Request: wrong file identifier/HTTP URL specified")
ErrWrongFileIDSymbol = NewAPIError(400, "Bad Request: wrong remote file id specified: can't unserialize it. Wrong last symbol")
ErrWrongFileIDLength = NewAPIError(400, "Bad Request: wrong remote file id specified: Wrong string length")
ErrWrongFileIDCharacter = NewAPIError(400, "Bad Request: wrong remote file id specified: Wrong character in the string")
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 an Array of String as options")
2020-04-05 17:23:51 +00:00
// No rights errors
ErrNoRightsToRestrict = NewAPIError(400, "Bad Request: not enough rights to restrict/unrestrict chat member")
2020-04-05 19:09:40 +00:00
ErrNoRightsToSend = NewAPIError(400, "Bad Request: have no rights to send a message")
2020-04-05 17:23:51 +00:00
ErrNoRightsToSendPhoto = NewAPIError(400, "Bad Request: not enough rights to send photos to the chat")
ErrNoRightsToSendStickers = NewAPIError(400, "Bad Request: not enough rights to send stickers to the chat")
ErrNoRightsToSendGifs = NewAPIError(400, "Bad Request: CHAT_SEND_GIFS_FORBIDDEN", "sending GIFS is not allowed in this chat")
ErrNoRightsToDelete = NewAPIError(400, "Bad Request: message can't be deleted")
ErrKickingChatOwner = NewAPIError(400, "Bad Request: can't remove chat owner")
2020-04-05 17:23:51 +00:00
// Super/groups errors
2020-04-05 19:09:40 +00:00
ErrBotKickedFromGroup = NewAPIError(403, "Forbidden: bot was kicked from the group chat")
ErrBotKickedFromSuperGroup = NewAPIError(403, "Forbidden: bot was kicked from the supergroup chat")
2020-03-27 23:05:46 +00:00
)
2020-04-05 17:23:51 +00:00
2020-04-05 18:29:05 +00:00
// ErrByDescription returns APIError instance by given description.
func ErrByDescription(s string) error {
2020-04-05 17:23:51 +00:00
switch s {
case ErrUnauthorized.ʔ():
return ErrUnauthorized
2020-04-27 21:16:21 +00:00
case ErrNotStartedByUser.ʔ():
return ErrNotStartedByUser
2020-04-05 18:29:05 +00:00
case ErrNotFound.ʔ():
return ErrNotFound
case ErrUserIsDeactivated.ʔ():
return ErrUserIsDeactivated
2020-04-05 17:23:51 +00:00
case ErrToForwardNotFound.ʔ():
return ErrToForwardNotFound
case ErrToReplyNotFound.ʔ():
return ErrToReplyNotFound
case ErrMessageTooLong.ʔ():
return ErrMessageTooLong
2020-04-05 19:09:40 +00:00
case ErrBlockedByUser.ʔ():
return ErrBlockedByUser
2020-04-05 17:23:51 +00:00
case ErrToDeleteNotFound.ʔ():
return ErrToDeleteNotFound
case ErrEmptyMessage.ʔ():
return ErrEmptyMessage
case ErrEmptyText.ʔ():
return ErrEmptyText
case ErrEmptyChatID.ʔ():
return ErrEmptyChatID
2020-04-05 19:09:40 +00:00
case ErrChatNotFound.ʔ():
return ErrChatNotFound
2020-04-05 17:23:51 +00:00
case ErrMessageNotModified.ʔ():
return ErrMessageNotModified
case ErrSameMessageContent.ʔ():
return ErrSameMessageContent
case ErrCantEditMessage.ʔ():
return ErrCantEditMessage
2020-04-23 18:55:52 +00:00
case ErrButtonDataInvalid.ʔ():
return ErrButtonDataInvalid
2020-04-24 14:52:15 +00:00
case ErrBadPollOptions.ʔ():
return ErrBadPollOptions
2020-04-05 17:23:51 +00:00
case ErrNoRightsToRestrict.ʔ():
return ErrNoRightsToRestrict
2020-04-05 19:09:40 +00:00
case ErrNoRightsToSend.ʔ():
return ErrNoRightsToSend
2020-04-05 17:23:51 +00:00
case ErrNoRightsToSendPhoto.ʔ():
return ErrNoRightsToSendPhoto
case ErrNoRightsToSendStickers.ʔ():
return ErrNoRightsToSendStickers
case ErrNoRightsToSendGifs.ʔ():
return ErrNoRightsToSendGifs
case ErrNoRightsToDelete.ʔ():
return ErrNoRightsToDelete
case ErrKickingChatOwner.ʔ():
return ErrKickingChatOwner
2020-04-05 19:09:40 +00:00
case ErrBotKickedFromGroup.ʔ():
2020-04-05 17:23:51 +00:00
return ErrKickingChatOwner
2020-04-05 19:09:40 +00:00
case ErrBotKickedFromSuperGroup.ʔ():
return ErrBotKickedFromSuperGroup
2020-04-05 17:23:51 +00:00
case ErrWrongTypeOfContent.ʔ():
return ErrWrongTypeOfContent
2020-04-05 19:09:40 +00:00
case ErrBadURLContent.ʔ():
return ErrBadURLContent
case ErrWrongFileIDSymbol.ʔ():
return ErrWrongFileIDSymbol
case ErrWrongFileIDLength.ʔ():
return ErrWrongFileIDLength
case ErrWrongFileIDCharacter.ʔ():
return ErrWrongFileIDCharacter
case ErrWrongFileID.ʔ():
return ErrWrongFileID
2020-04-05 17:23:51 +00:00
case ErrTooLarge.ʔ():
return ErrTooLarge
2020-04-05 19:09:40 +00:00
case ErrWrongFileIDPadding.ʔ():
return ErrWrongFileIDPadding
case ErrFailedImageProcess.ʔ():
return ErrFailedImageProcess
case ErrInvalidStickerSet.ʔ():
return ErrInvalidStickerSet
2020-04-05 17:23:51 +00:00
default:
return nil
}
}