Add error parameters from JSON error to APIError

pull/382/head
Enrico204 4 years ago committed by Demian
parent 844bbc5c97
commit d56841d3dc

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

@ -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
}
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)
err = FloodError{
APIError: NewAPIError(429, tgramApiError.Description),
RetryAfter: retryAfterInt,
}
default:
err = fmt.Errorf("telegram unknown: %s (%d)", tgramApiError.Description, tgramApiError.ErrorCode)
}
return err

Loading…
Cancel
Save