From 737f2fc86da16d459cd38625ad5f3acb927e83b1 Mon Sep 17 00:00:00 2001 From: Edwin Date: Tue, 14 Nov 2023 15:26:07 +0800 Subject: [PATCH] pkg/exchange: add response validator --- pkg/exchange/bitget/bitgetapi/client.go | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/pkg/exchange/bitget/bitgetapi/client.go b/pkg/exchange/bitget/bitgetapi/client.go index 8fa7f4404..a85b07af3 100644 --- a/pkg/exchange/bitget/bitgetapi/client.go +++ b/pkg/exchange/bitget/bitgetapi/client.go @@ -7,6 +7,7 @@ import ( "crypto/sha256" "encoding/base64" "encoding/json" + "fmt" "net/http" "net/url" "strconv" @@ -164,3 +165,17 @@ type APIResponse struct { Message string `json:"msg"` Data json.RawMessage `json:"data"` } + +func (a APIResponse) Validate() error { + // v1, v2 use the same success code. + // https://www.bitget.com/api-doc/spot/error-code/restapi + // https://bitgetlimited.github.io/apidoc/en/mix/#restapi-error-codes + if a.Code != "00000" { + return a.Error() + } + return nil +} + +func (a APIResponse) Error() error { + return fmt.Errorf("code: %s, msg: %s, data: %q", a.Code, a.Message, a.Data) +}