-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved handling of ratelimits & errors
- Loading branch information
Altarrel
committed
Jun 18, 2018
1 parent
38c6651
commit bf89c67
Showing
2 changed files
with
48 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package goroyale | ||
|
||
import ( | ||
"fmt" | ||
"time" | ||
) | ||
|
||
// APIError represents an error returned from the API. | ||
// https://docs.royaleapi.com/#/errors | ||
type APIError struct { | ||
StatusCode int `json:"status"` // http reponse code | ||
Message string // human readable message explaining the error | ||
} | ||
|
||
func (err APIError) Error() string { | ||
return err.Message | ||
} | ||
|
||
// RatelimitError is returned when it is detected that you will hit the ratelimit. | ||
type RatelimitError struct { | ||
RetryAfter time.Duration | ||
} | ||
|
||
func (err RatelimitError) Error() string { | ||
return fmt.Sprintf("ratelimit, retry in: %v", err.RetryAfter) | ||
} | ||
|
||
func newRatelimitError(retryAfter time.Duration) *RatelimitError { | ||
err := &RatelimitError{ | ||
RetryAfter: retryAfter, | ||
} | ||
return err | ||
} |