Skip to content

Commit

Permalink
BREAKING CHANGE: new errors
Browse files Browse the repository at this point in the history
  • Loading branch information
SevereCloud committed Aug 2, 2020
1 parent dd54738 commit 6ac648a
Show file tree
Hide file tree
Showing 12 changed files with 506 additions and 678 deletions.
49 changes: 12 additions & 37 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"sync"
"time"

"github.com/SevereCloud/vksdk/api/errors"
"github.com/SevereCloud/vksdk/internal"
"github.com/SevereCloud/vksdk/object"
)
Expand Down Expand Up @@ -95,24 +94,11 @@ type VK struct {
rps int
}

// Error struct VK.
//
// Deprecated: use object.Error.
type Error struct {
Code int `json:"error_code"`
Message string `json:"error_msg"`
Text string `json:"error_text"`
CaptchaSID string `json:"captcha_sid"`
CaptchaImg string `json:"captcha_img"`
ConfirmationText string `json:"confirmation_text"` // text of the message to be shown in the default confirmation window.
RequestParams []object.BaseRequestParam `json:"request_params"`
}

// Response struct.
type Response struct {
Response json.RawMessage `json:"response"`
Error object.Error `json:"error"`
ExecuteErrors []object.ExecuteError `json:"execute_errors"`
Response json.RawMessage `json:"response"`
Error Error `json:"error"`
ExecuteErrors ExecuteErrors `json:"execute_errors"`
}

// NewVK returns a new VK.
Expand Down Expand Up @@ -265,16 +251,18 @@ func (vk *VK) defaultHandler(method string, params Params) (Response, error) {

_ = resp.Body.Close()

err = errors.New(response.Error)
if err != nil {
if errors.GetType(err) == errors.TooMany && attempt < vk.Limit {
switch response.Error.Code {
case ErrNoType:
return response, nil
case ErrTooMany:
if attempt < vk.Limit {
continue
}

return response, err
return response, response.Error
}

return response, nil
return response, response.Error
}
}

Expand Down Expand Up @@ -341,21 +329,8 @@ func (vk *VK) ExecuteWithArgs(code string, params Params, obj interface{}) error
copyParams["v"] = vk.Version

resp, err := vk.Handler("execute", copyParams)

// Add execute errors
for _, executeError := range resp.ExecuteErrors {
context := object.Error{
Code: executeError.ErrorCode,
Message: executeError.ErrorMsg,
RequestParams: []object.BaseRequestParam{
{
Key: "method",
Value: executeError.Method,
},
},
}

err = errors.AddErrorContext(err, context)
if resp.ExecuteErrors != nil {
return resp.ExecuteErrors
}

jsonErr := json.Unmarshal(resp.Response, &obj)
Expand Down
39 changes: 17 additions & 22 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,42 +9,36 @@ import (
"testing"

"github.com/SevereCloud/vksdk/api"
"github.com/SevereCloud/vksdk/api/errors"
"github.com/SevereCloud/vksdk/object"
"github.com/stretchr/testify/assert"
)

func noError(t *testing.T, err error) bool {
t.Helper()

switch errors.GetType(err) {
// case errors.TooMany:
// t.Skip("Too many requests per second")
case errors.Server:
t.Skip("Internal server error")
case errors.Permission:
t.Skip("Permission to perform this action is denied")
case errors.Captcha:
t.Skip("Captcha needed")
}

if err != nil {
ctx := errors.GetErrorContext(err)
if ctx.Code != 0 {
if e, ok := err.(*api.Error); ok {
switch e.Code {
case api.ErrServer:
t.Skip("Internal server error")
case api.ErrPermission:
t.Skip("Permission to perform this action is denied")
case api.ErrCaptcha:
t.Skip("Captcha needed")
default:
s := "\n"
s += fmt.Sprintf("code: %d\n", ctx.Code)
s += fmt.Sprintf("text: %s\n", ctx.Text)
s += fmt.Sprintf("message: %s\n", ctx.Message)
s += fmt.Sprintf("code: %d\n", e.Code)
s += fmt.Sprintf("text: %s\n", e.Text)
s += fmt.Sprintf("message: %s\n", e.Message)
s += "params:\n"

for _, param := range ctx.RequestParams {
for _, param := range e.RequestParams {
s += fmt.Sprintf("\t%s: %s\n", param.Key, param.Value)
}

t.Log(s)
} else {
t.Log(fmt.Sprintf("\n%#v", err))
}
} else {
t.Log(fmt.Sprintf("\n%#v", err))
}

return assert.NoError(t, err)
Expand Down Expand Up @@ -305,7 +299,8 @@ func TestVK_CaptchaForce(t *testing.T) {
needUserToken(t)

_, err := vkUser.CaptchaForce(api.Params{})
if errors.GetType(err) != errors.Captcha {

if e, ok := err.(*api.Error); err != nil || !ok || e.Code != api.ErrCaptcha {
t.Errorf("VK.CaptchaForce() err=%v, want 14", err)
}
}
Expand Down
Loading

0 comments on commit 6ac648a

Please sign in to comment.