Skip to content

Commit

Permalink
add error info from API response
Browse files Browse the repository at this point in the history
  • Loading branch information
Sagleft committed Feb 14, 2023
1 parent d144bde commit b8c7f1c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
4 changes: 4 additions & 0 deletions structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type BalanceData struct {
// APITradeResponse - ..
type APITradeResponse struct {
Success bool `json:"success"`
Error string `json:"text"`
Result APITradeData `json:"result"`
}

Expand All @@ -87,6 +88,7 @@ type APITradeData struct {
// APIPairsResponse - ..
type APIPairsResponse struct {
Success bool `json:"success"`
Error string `json:"text"`
Result APIPairsData `json:"result"`
}

Expand Down Expand Up @@ -149,6 +151,7 @@ type CurrencyData struct {
// APIBookValueResponse - ..
type APIBookValueResponse struct {
Success bool `json:"success"`
Error string `json:"text"`
Result BookValueDataContainer `json:"result"`
}

Expand All @@ -168,6 +171,7 @@ type BookValueData struct {
// APICurrenciesListResponse - ..
type APICurrenciesListResponse struct {
Success bool `json:"success"`
Error string `json:"text"`
Result CurrenciesListData `json:"result"`
}

Expand Down
19 changes: 10 additions & 9 deletions trade.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package uexchange
import (
"encoding/json"
"errors"
"fmt"
"net/url"
"strconv"
)
Expand All @@ -22,11 +23,11 @@ func (c *Client) sendTradeTask(orderType string, pairSymbol string, amount, pric
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return 0, errors.New("failed to decode request response: " + err.Error())
return 0, errors.New("decode request response: " + err.Error())
}

if !response.Success {
return 0, errors.New("failed to send trade task") // TODO
return 0, fmt.Errorf("send trade task: %s", response.Error)
}
return response.Result.OrderID, nil
}
Expand Down Expand Up @@ -54,11 +55,11 @@ func (c *Client) Hold(orderID int64) error {
var response APITradeResponse
err = json.Unmarshal(body, &response)
if err != nil {
return errors.New("failed to decode request response: " + err.Error())
return errors.New("decode request response: " + err.Error())
}

if !response.Success {
return errors.New("failed to send trade task") // TODO
return fmt.Errorf("send trade task: %s", response.Error)
}
return nil
}
Expand All @@ -80,7 +81,7 @@ func (c *Client) Cancel(orderID int64) error {
}

if !response.Success {
return errors.New("failed to send trade task") // TODO
return fmt.Errorf("send trade task: %s", response.Error)
}
return nil
}
Expand All @@ -100,7 +101,7 @@ func (c *Client) GetPairs() ([]PairsDataContainer, error) {
}

if !response.Success {
return nil, errors.New("failed to get pairs list") // TODO
return nil, fmt.Errorf("send trade task: %s", response.Error)
}
return response.Result.Pairs, nil
}
Expand All @@ -122,7 +123,7 @@ func (c *Client) GetOrderBook(pairSymbol string) (*BookValueDataContainer, error
}

if !response.Success {
return nil, errors.New("failed to get order book by pair") // TODO
return nil, fmt.Errorf("send trade task: %s", response.Error)
}
return &response.Result, nil
}
Expand All @@ -144,7 +145,7 @@ func (c *Client) GetMarketCurrenciesList(pairSymbol string) (*CurrenciesListData
}

if !response.Success {
return nil, errors.New("failed to get currencies list") // TODO
return nil, fmt.Errorf("send trade task: %s", response.Error)
}
return &response.Result, nil
}
Expand Down Expand Up @@ -178,5 +179,5 @@ func (c *Client) GetPairPrice(pairCode string) (PairPriceData, error) {
}
}

return result, errors.New("pair " + pairCode + " not found")
return result, fmt.Errorf("pair %s not found", pairCode)
}

0 comments on commit b8c7f1c

Please sign in to comment.