mirror of
https://github.com/elyby/chrly.git
synced 2024-12-23 21:50:03 +05:30
Handle 403 Forbidden error from Mojang's API
This commit is contained in:
parent
334e60ff2f
commit
b2a1fd450b
@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|||||||
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
||||||
|
|
||||||
## [Unreleased] - xxxx-xx-xx
|
## [Unreleased] - xxxx-xx-xx
|
||||||
|
### Added
|
||||||
|
- 403 Forbidden errors from the Mojang's API are now logged
|
||||||
|
|
||||||
## [4.2.3] - 2019-10-03
|
## [4.2.3] - 2019-10-03
|
||||||
### Changed
|
### Changed
|
||||||
|
@ -125,6 +125,8 @@ func validateResponse(response *http.Response) error {
|
|||||||
_ = json.Unmarshal(body, &decodedError)
|
_ = json.Unmarshal(body, &decodedError)
|
||||||
|
|
||||||
return &BadRequestError{ErrorType: decodedError.Error, Message: decodedError.Message}
|
return &BadRequestError{ErrorType: decodedError.Error, Message: decodedError.Message}
|
||||||
|
case response.StatusCode == 403:
|
||||||
|
return &ForbiddenError{}
|
||||||
case response.StatusCode == 429:
|
case response.StatusCode == 429:
|
||||||
return &TooManyRequestsError{}
|
return &TooManyRequestsError{}
|
||||||
case response.StatusCode >= 500:
|
case response.StatusCode >= 500:
|
||||||
@ -166,6 +168,15 @@ func (*BadRequestError) IsMojangError() bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// When Mojang decides you're such a bad guy, this error appears (even if the request has no authorization)
|
||||||
|
type ForbiddenError struct {
|
||||||
|
ResponseError
|
||||||
|
}
|
||||||
|
|
||||||
|
func (*ForbiddenError) Error() string {
|
||||||
|
return "Forbidden"
|
||||||
|
}
|
||||||
|
|
||||||
// When you exceed the set limit of requests, this error will be returned
|
// When you exceed the set limit of requests, this error will be returned
|
||||||
type TooManyRequestsError struct {
|
type TooManyRequestsError struct {
|
||||||
ResponseError
|
ResponseError
|
||||||
|
@ -102,6 +102,27 @@ func TestUsernamesToUuids(t *testing.T) {
|
|||||||
assert.Implements((*ResponseError)(nil), err)
|
assert.Implements((*ResponseError)(nil), err)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("handle forbidden response", func(t *testing.T) {
|
||||||
|
assert := testify.New(t)
|
||||||
|
|
||||||
|
defer gock.Off()
|
||||||
|
gock.New("https://api.mojang.com").
|
||||||
|
Post("/profiles/minecraft").
|
||||||
|
Reply(403).
|
||||||
|
BodyString("just because")
|
||||||
|
|
||||||
|
client := &http.Client{}
|
||||||
|
gock.InterceptClient(client)
|
||||||
|
|
||||||
|
HttpClient = client
|
||||||
|
|
||||||
|
result, err := UsernamesToUuids([]string{"Thinkofdeath", "maksimkurb"})
|
||||||
|
assert.Nil(result)
|
||||||
|
assert.IsType(&ForbiddenError{}, err)
|
||||||
|
assert.EqualError(err, "Forbidden")
|
||||||
|
assert.Implements((*ResponseError)(nil), err)
|
||||||
|
})
|
||||||
|
|
||||||
t.Run("handle too many requests response", func(t *testing.T) {
|
t.Run("handle too many requests response", func(t *testing.T) {
|
||||||
assert := testify.New(t)
|
assert := testify.New(t)
|
||||||
|
|
||||||
|
@ -182,13 +182,18 @@ func (ctx *JobsQueue) handleResponseError(err error, threadName string) {
|
|||||||
|
|
||||||
switch err.(type) {
|
switch err.(type) {
|
||||||
case mojang.ResponseError:
|
case mojang.ResponseError:
|
||||||
if _, ok := err.(*mojang.TooManyRequestsError); ok {
|
if _, ok := err.(*mojang.BadRequestError); ok {
|
||||||
ctx.Logger.Warning(":name: Got 429 Too Many Requests :err", wd.NameParam(threadName), wd.ErrParam(err))
|
ctx.Logger.Warning(":name: Got 400 Bad Request :err", wd.NameParam(threadName), wd.ErrParam(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, ok := err.(*mojang.BadRequestError); ok {
|
if _, ok := err.(*mojang.ForbiddenError); ok {
|
||||||
ctx.Logger.Warning(":name: Got 400 Bad Request :err", wd.NameParam(threadName), wd.ErrParam(err))
|
ctx.Logger.Warning(":name: Got 403 Forbidden :err", wd.NameParam(threadName), wd.ErrParam(err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := err.(*mojang.TooManyRequestsError); ok {
|
||||||
|
ctx.Logger.Warning(":name: Got 429 Too Many Requests :err", wd.NameParam(threadName), wd.ErrParam(err))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -403,6 +403,7 @@ func (*timeoutError) Temporary() bool { return false }
|
|||||||
|
|
||||||
var expectedErrors = []error{
|
var expectedErrors = []error{
|
||||||
&mojang.BadRequestError{},
|
&mojang.BadRequestError{},
|
||||||
|
&mojang.ForbiddenError{},
|
||||||
&mojang.TooManyRequestsError{},
|
&mojang.TooManyRequestsError{},
|
||||||
&mojang.ServerError{},
|
&mojang.ServerError{},
|
||||||
&timeoutError{},
|
&timeoutError{},
|
||||||
@ -418,6 +419,7 @@ func (suite *queueTestSuite) TestShouldNotLogErrorWhenExpectedErrorReturnedFromU
|
|||||||
suite.Logger.On("RecordTimer", mock.Anything, mock.Anything)
|
suite.Logger.On("RecordTimer", mock.Anything, mock.Anything)
|
||||||
suite.Logger.On("Debug", ":name: Got response error :err", mock.Anything, mock.Anything).Times(len(expectedErrors))
|
suite.Logger.On("Debug", ":name: Got response error :err", mock.Anything, mock.Anything).Times(len(expectedErrors))
|
||||||
suite.Logger.On("Warning", ":name: Got 400 Bad Request :err", mock.Anything, mock.Anything).Once()
|
suite.Logger.On("Warning", ":name: Got 400 Bad Request :err", mock.Anything, mock.Anything).Once()
|
||||||
|
suite.Logger.On("Warning", ":name: Got 403 Forbidden :err", mock.Anything, mock.Anything).Once()
|
||||||
suite.Logger.On("Warning", ":name: Got 429 Too Many Requests :err", mock.Anything, mock.Anything).Once()
|
suite.Logger.On("Warning", ":name: Got 429 Too Many Requests :err", mock.Anything, mock.Anything).Once()
|
||||||
|
|
||||||
suite.Storage.On("GetUuid", "maksimkurb").Return("", &ValueNotFound{})
|
suite.Storage.On("GetUuid", "maksimkurb").Return("", &ValueNotFound{})
|
||||||
@ -455,6 +457,7 @@ func (suite *queueTestSuite) TestShouldNotLogErrorWhenExpectedErrorReturnedFromU
|
|||||||
suite.Logger.On("RecordTimer", mock.Anything, mock.Anything)
|
suite.Logger.On("RecordTimer", mock.Anything, mock.Anything)
|
||||||
suite.Logger.On("Debug", ":name: Got response error :err", mock.Anything, mock.Anything).Times(len(expectedErrors))
|
suite.Logger.On("Debug", ":name: Got response error :err", mock.Anything, mock.Anything).Times(len(expectedErrors))
|
||||||
suite.Logger.On("Warning", ":name: Got 400 Bad Request :err", mock.Anything, mock.Anything).Once()
|
suite.Logger.On("Warning", ":name: Got 400 Bad Request :err", mock.Anything, mock.Anything).Once()
|
||||||
|
suite.Logger.On("Warning", ":name: Got 403 Forbidden :err", mock.Anything, mock.Anything).Once()
|
||||||
suite.Logger.On("Warning", ":name: Got 429 Too Many Requests :err", mock.Anything, mock.Anything).Once()
|
suite.Logger.On("Warning", ":name: Got 429 Too Many Requests :err", mock.Anything, mock.Anything).Once()
|
||||||
|
|
||||||
suite.Storage.On("GetUuid", "maksimkurb").Return("", &ValueNotFound{})
|
suite.Storage.On("GetUuid", "maksimkurb").Return("", &ValueNotFound{})
|
||||||
|
Loading…
Reference in New Issue
Block a user