From 2b34a0900c3640a994a3f75b2927382018b8c932 Mon Sep 17 00:00:00 2001 From: Carlosokumu Date: Thu, 24 Oct 2024 16:35:53 +0300 Subject: [PATCH] check for status code and unmarshal on errResponse --- internal/handlers/server/accountservice.go | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/handlers/server/accountservice.go b/internal/handlers/server/accountservice.go index 9534520..27896e7 100644 --- a/internal/handlers/server/accountservice.go +++ b/internal/handlers/server/accountservice.go @@ -84,14 +84,17 @@ func (as *AccountService) TrackAccountStatus(publicKey string) (*api.OKResponse, errResponse.Description = err.Error() return nil, err } - err = json.Unmarshal([]byte(body), &okResponse) - if err != nil { + if resp.StatusCode >= http.StatusBadRequest { err := json.Unmarshal([]byte(body), &errResponse) if err != nil { return nil, err } return nil, errors.New(errResponse.Description) } + err = json.Unmarshal([]byte(body), &okResponse) + if err != nil { + return nil, err + } if len(okResponse.Result) == 0 { return nil, errors.New("Empty api result") } @@ -148,15 +151,17 @@ func (as *AccountService) CreateAccount() (*api.OKResponse, error) { if err != nil { return nil, err } - err = json.Unmarshal([]byte(body), &okResponse) - - if err != nil { + if resp.StatusCode >= http.StatusBadRequest { err := json.Unmarshal([]byte(body), &errResponse) if err != nil { return nil, err } return nil, errors.New(errResponse.Description) } + err = json.Unmarshal([]byte(body), &okResponse) + if err != nil { + return nil, err + } if len(okResponse.Result) == 0 { return nil, errors.New("Empty api result") }