1
bot/errors.go
Ehson e65d48be16
Added error handling for specific error codes (#69)
* Added error handling for specific error codes

* added TooManyRequestsError witch have retry_after

* Added a parameters field with retry_after and handled error code 429

* added example of handling TooManyRequestsError with RetryAfter

* fix: changed description

* fix: used switch statement instead of if statement

* add MigrateError type and IsMigrateError function

* updated apiResponse struct and error handling logic

* fix: changed from %w to %s to ensure proper functionality of Sprintf

* fix: correct error message in getMe test

---------

Co-authored-by: Ehson <priler05@gmail.com>
Co-authored-by: usmonzodasomon <budushiyprogrammist@gmail.com>
2024-04-03 10:45:48 +03:00

44 lines
918 B
Go

package bot
import (
"errors"
"fmt"
)
var (
ErrorForbidden = errors.New("forbidden")
ErrorBadRequest = errors.New("bad request")
ErrorUnauthorized = errors.New("unauthorized")
ErrorTooManyRequests = errors.New("too many requests")
ErrorNotFound = errors.New("not found")
ErrorConflict = errors.New("conflict")
)
type TooManyRequestsError struct {
Message string
RetryAfter int
}
func (e *TooManyRequestsError) Error() string {
return fmt.Sprintf("%s: retry_after %d", e.Message, e.RetryAfter)
}
func IsTooManyRequestsError(err error) bool {
_, ok := err.(*TooManyRequestsError)
return ok
}
type MigrateError struct {
Message string
MigrateToChatID int
}
func (e *MigrateError) Error() string {
return fmt.Sprintf("%s: migrate_to_chat_id %d", e.Message, e.MigrateToChatID)
}
func IsMigrateError(err error) bool {
_, ok := err.(*MigrateError)
return ok
}