Add error parameters from JSON error to APIError

This commit is contained in:
Enrico204 2020-11-15 17:57:42 +01:00 committed by Demian
parent 844bbc5c97
commit d56841d3dc
2 changed files with 22 additions and 14 deletions

View File

@ -9,6 +9,7 @@ type APIError struct {
Code int
Description string
Message string
Parameters map[string]interface{}
}
type FloodError struct {

35
util.go
View File

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