mirror of
https://github.com/tucnak/telebot
synced 2024-11-05 06:00:58 +00:00
Add error parameters from JSON error to APIError
This commit is contained in:
parent
844bbc5c97
commit
d56841d3dc
@ -9,6 +9,7 @@ type APIError struct {
|
||||
Code int
|
||||
Description string
|
||||
Message string
|
||||
Parameters map[string]interface{}
|
||||
}
|
||||
|
||||
type FloodError struct {
|
||||
|
35
util.go
35
util.go
@ -70,22 +70,29 @@ func extractOk(data []byte) error {
|
||||
}
|
||||
|
||||
err = ErrByDescription(tgramApiError.Description)
|
||||
if err == nil {
|
||||
switch tgramApiError.ErrorCode {
|
||||
case http.StatusTooManyRequests:
|
||||
retryAfter, ok := tgramApiError.Parameters["retry_after"]
|
||||
if !ok {
|
||||
return NewAPIError(429, tgramApiError.Description)
|
||||
}
|
||||
retryAfterInt, _ := strconv.Atoi(fmt.Sprint(retryAfter))
|
||||
if err != nil {
|
||||
apierr, _ := err.(*APIError)
|
||||
// Formally this is wrong, as the error is not created on the fly
|
||||
// However, given the current way of handling errors, this a working
|
||||
// workaround which doesn't break the API
|
||||
apierr.Parameters = tgramApiError.Parameters
|
||||
return apierr
|
||||
}
|
||||
|
||||
err = FloodError{
|
||||
APIError: NewAPIError(429, tgramApiError.Description),
|
||||
RetryAfter: retryAfterInt,
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("telegram unknown: %s (%d)", tgramApiError.Description, tgramApiError.ErrorCode)
|
||||
switch tgramApiError.ErrorCode {
|
||||
case http.StatusTooManyRequests:
|
||||
retryAfter, ok := tgramApiError.Parameters["retry_after"]
|
||||
if !ok {
|
||||
return NewAPIError(429, tgramApiError.Description)
|
||||
}
|
||||
retryAfterInt, _ := strconv.Atoi(fmt.Sprint(retryAfter))
|
||||
|
||||
err = FloodError{
|
||||
APIError: NewAPIError(429, tgramApiError.Description),
|
||||
RetryAfter: retryAfterInt,
|
||||
}
|
||||
default:
|
||||
err = fmt.Errorf("telegram unknown: %s (%d)", tgramApiError.Description, tgramApiError.ErrorCode)
|
||||
}
|
||||
|
||||
return err
|
||||
|
Loading…
Reference in New Issue
Block a user