hirokisan / bybit

Bybit client library for Go

Home Page:https://pkg.go.dev/github.com/hirokisan/bybit/v2

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

USDTPerp ListLinearPositions doesn't return errors properly

rtunazzz opened this issue · comments

commented

Problem

When calling the ListLinearPositions() method, Unmarshal errors are encountered when the API call does not succeed.

Root Cause

The Bybit API returns an empty result object {} in their response object when an error occurs. For instance, when an invalid API key is used:

{"ret_code":10005,"ret_msg":"Permission denied, please check your API key permissions.","result":{},"ext_code":"","ext_info":"","time_now":"1674306441.372007","rate_limit_status":0,"rate_limit":0,"rate_limit_reset_ms":0}

This value cannot be properly Unmarshalled into an array-like struct, resulting in an error such as:

json: cannot unmarshal object into Go struct field ListLinearPositionsResponse.result of type []bybit.ListLinearPositionsResult
commented

I propose adding a custom Unmarshaler so that the errors are returned properly:

func (b *ListLinearPositionsResponse) UnmarshalJSON(data []byte) error {
	var v interface{}
	if err := json.Unmarshal(data, &v); err != nil {
		return err
	}

	b.CommonResponse, _ = v.(CommonResponse)
	b.Result, _ = v.([]ListLinearPositionsResult)

	return nil
}

After this change, the error return value is:

10005, Permission denied, please check your API key permissions.

Let me know if you're good with that @hirokisan so I can submit a PR :)

It was a client response handling issue.

I tried to fix the issue here #72

If you have time, look at the changes.

I closed the issue when I merged the PR.
If you have any problems, please re-open the issue.

Thanks for your committing, again!