Merge pull request #294 from aofei/v2

errors: fix regular expression
This commit is contained in:
demget 2020-05-18 21:47:50 +03:00 committed by GitHub
commit fa41701896
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 1 deletions

View File

@ -45,7 +45,7 @@ func NewAPIError(code int, msgs ...string) *APIError {
return err return err
} }
var errorRx = regexp.MustCompile(`{.+"error_code":(\d+),"description":"(.+)"}`) var errorRx = regexp.MustCompile(`{.+"error_code":(\d+),"description":"(.+)".*}`)
var ( var (
// General errors // General errors

18
util_test.go Normal file
View File

@ -0,0 +1,18 @@
package telebot
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestExtractOk(t *testing.T) {
data := []byte(`{"ok":true,"result":{"foo":"bar"}}`)
assert.NoError(t, extractOk(data))
data = []byte(`{"ok":false,"error_code":429,"description":"Too Many Requests: retry after 8","parameters":{"retry_after":8}}`)
assert.Error(t, extractOk(data))
data = []byte(`{"ok":false,"error_code":400,"description":"Bad Request: reply message not found"}`) // Also check the old format
assert.EqualError(t, extractOk(data), ErrToReplyNotFound.Error())
}