-
Notifications
You must be signed in to change notification settings - Fork 68
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <[email protected]> Co-authored-by: usmonzodasomon <[email protected]>
- Loading branch information
1 parent
8f8421f
commit e65d48b
Showing
4 changed files
with
106 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters