Skip to content

Commit

Permalink
cleanup: convert to helper in client for logging unmarshal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaportPhilipBrowne committed Oct 1, 2024
1 parent 098d181 commit 8403d6a
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 53 deletions.
10 changes: 10 additions & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,16 @@ func (c *Client) Do(ctx context.Context, req *http.Request, v interface{}) (*htt
return resp, nil
}

func (c *Client) LogJSONUnmarshalError(ctx context.Context, responseBody string, responseType string, respSchema interface{}, err error) {
expectedJSON, _ := json.Marshal(respSchema)
c.Logger.DebugContext(ctx, "error unmarshaling response body",
slog.String("response_body", responseBody),
slog.String("response_type", responseType),
slog.String("expected_json_schema", fmt.Sprintf("%+v", expectedJSON)),
slog.String("error", err.Error()),
)
}

type AuthInfo struct {
Expiration time.Time
AccessToken string
Expand Down
9 changes: 4 additions & 5 deletions location.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"

"github.com/lithammer/fuzzysearch/fuzzy"
Expand Down Expand Up @@ -154,8 +153,8 @@ func (svc *LocationServiceOp) ListLocations(ctx context.Context) ([]*Location, e

unmarshalErr := json.Unmarshal(body, locationResponse)
if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(locationResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_locations_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
locationRes := LocationResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_locations_response", locationRes, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -235,8 +234,8 @@ func (svc *LocationServiceOp) ListCountries(ctx context.Context) ([]*Country, er
unmarshalErr := json.Unmarshal(body, &countryResponse)

if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(countryResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_countries_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
countryResp := CountryResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_countries_response", countryResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down
13 changes: 6 additions & 7 deletions managed_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
)

type ManagedAccountService interface {
Expand Down Expand Up @@ -74,8 +73,8 @@ func (svc *ManagedAccountServiceOp) ListManagedAccounts(ctx context.Context) ([]
var apiResponse *ManagedAccountListAPIResponse

if err := json.Unmarshal(body, &apiResponse); err != nil {
expectedJSON, _ := json.Marshal(apiResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_managed_accounts_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
managedAccountsAPIRes := ManagedAccountListAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_managed_accounts_response", managedAccountsAPIRes, err)
return nil, err
}

Expand All @@ -99,8 +98,8 @@ func (svc *ManagedAccountServiceOp) CreateManagedAccount(ctx context.Context, re
}
var createManagedAccountResponse *ManagedAccountAPIResponse
if err := json.Unmarshal(body, &createManagedAccountResponse); err != nil {
expectedJSON, _ := json.Marshal(createManagedAccountResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "create_managed_account_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
managedAccountResponse := ManagedAccountAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "create_managed_account_response", managedAccountResponse, err)
return nil, err
}
return createManagedAccountResponse.Data, nil
Expand All @@ -123,8 +122,8 @@ func (svc *ManagedAccountServiceOp) UpdateManagedAccount(ctx context.Context, co
}
var updateManagedAccountResponse *ManagedAccountAPIResponse
if err := json.Unmarshal(body, &updateManagedAccountResponse); err != nil {
expectedJSON, _ := json.Marshal(updateManagedAccountResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "update_managed_account_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
updateManagedAccountResponse := ManagedAccountAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "update_managed_account_response", updateManagedAccountResponse, err)
return nil, err
}
return updateManagedAccountResponse.Data, nil
Expand Down
21 changes: 10 additions & 11 deletions mcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"slices"
"strconv"
"time"
Expand Down Expand Up @@ -146,8 +145,8 @@ func (svc *MCRServiceOp) BuyMCR(ctx context.Context, req *BuyMCRRequest) (*BuyMC
unmarshalErr := json.Unmarshal(*body, &orderInfo)

if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(orderInfo)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(*body)), slog.String("response_type", "mcr_order_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
orderResponse := MCROrderResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(*body), "buy_mcr_response", orderResponse, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -258,8 +257,8 @@ func (svc *MCRServiceOp) GetMCR(ctx context.Context, mcrId string) (*MCR, error)
unmarshalErr := json.Unmarshal(body, mcrRes)

if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(mcrRes)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "get_mcr_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
mcrResp := MCRResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_mcr_response", mcrResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -291,8 +290,8 @@ func (svc *MCRServiceOp) CreatePrefixFilterList(ctx context.Context, req *Create
createRes := &APIMCRPrefixFilterListResponse{}
unmarshalErr := json.Unmarshal(body, createRes)
if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(createRes)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "create_mcr_prefix_filter_list_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
createResp := APIMCRPrefixFilterListResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "create_mcr_prefix_filter_list_response", createResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -335,8 +334,8 @@ func (svc *MCRServiceOp) ListMCRPrefixFilterLists(ctx context.Context, mcrId str
unmarshalErr := json.Unmarshal(body, prefixFilterList)

if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(prefixFilterList)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_mcr_prefix_filter_list_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
listPrefixFilterListResp := ListMCRPrefixFilterListResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_mcr_prefix_filter_list_response", listPrefixFilterListResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -367,8 +366,8 @@ func (svc *MCRServiceOp) GetMCRPrefixFilterList(ctx context.Context, mcrID strin
apiPrefixFilterList := &APIMCRPrefixFilterListResponse{}
unmarshalErr := json.Unmarshal(body, apiPrefixFilterList)
if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(apiPrefixFilterList)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "get_mcr_prefix_filter_list_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
apiPrefixFilterListResp := APIMCRPrefixFilterListResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_mcr_prefix_filter_list_response", apiPrefixFilterListResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down
17 changes: 8 additions & 9 deletions mve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -105,8 +104,8 @@ func (svc *MVEServiceOp) BuyMVE(ctx context.Context, req *BuyMVERequest) (*BuyMV
orderInfo := MVEOrderResponse{}

if err := json.Unmarshal(*resp, &orderInfo); err != nil {
expectedJSON, _ := json.Marshal(orderInfo)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(*resp)), slog.String("response_type", "order_mve_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
mveResp := MVEOrderResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(*resp), "buy_mve_response", mveResp, err)
return nil, err
}

Expand Down Expand Up @@ -201,8 +200,8 @@ func (svc *MVEServiceOp) GetMVE(ctx context.Context, mveId string) (*MVE, error)
}
mveResp := MVEResponse{}
if err := json.Unmarshal(body, &mveResp); err != nil {
expectedJSON, _ := json.Marshal(mveResp)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "get_mve_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
getMVERes := MVEResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_mve_response", getMVERes, err)
return nil, err
}

Expand Down Expand Up @@ -295,8 +294,8 @@ func (svc *MVEServiceOp) ListMVEImages(ctx context.Context) ([]*MVEImage, error)
}
imageResp := MVEImageAPIResponse{}
if err := json.Unmarshal(body, &imageResp); err != nil {
expectedJSON, _ := json.Marshal(imageResp)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_mve_images_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
listImageRes := MVEImageAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_mve_images_response", listImageRes, err)
return nil, err
}
return imageResp.Data.Images, nil
Expand All @@ -320,8 +319,8 @@ func (svc *MVEServiceOp) ListAvailableMVESizes(ctx context.Context) ([]*MVESize,
}
sizeResp := MVESizeAPIResponse{}
if err := json.Unmarshal(body, &sizeResp); err != nil {
expectedJSON, _ := json.Marshal(sizeResp)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_mve_sizes_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
listSizesResp := MVESizeAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_mve_sizes_response", listSizesResp, err)
return nil, err
}
return sizeResp.Data, nil
Expand Down
5 changes: 2 additions & 3 deletions partner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"

"github.com/lithammer/fuzzysearch/fuzzy"
Expand Down Expand Up @@ -59,8 +58,8 @@ func (svc *PartnerServiceOp) ListPartnerMegaports(ctx context.Context) ([]*Partn
partnerMegaportResponse := PartnerMegaportResponse{}
unmarshalErr := json.Unmarshal(body, &partnerMegaportResponse)
if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(partnerMegaportResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_partner_ports_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
partnerResp := PartnerMegaportResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_partner_megaports_response", partnerResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down
13 changes: 6 additions & 7 deletions port.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"slices"
Expand Down Expand Up @@ -147,8 +146,8 @@ func (svc *PortServiceOp) BuyPort(ctx context.Context, req *BuyPortRequest) (*Bu
orderInfo := PortOrderResponse{}
unmarshalErr := json.Unmarshal(*responseBody, &orderInfo)
if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(buyOrder)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(*responseBody)), slog.String("response_type", "port_order_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
portOrderResp := PortOrderResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(*responseBody), "buy_port_response", portOrderResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -261,8 +260,8 @@ func (svc *PortServiceOp) ListPorts(ctx context.Context) ([]*Port, error) {
unmarshalErr := json.Unmarshal(body, &parsed)

if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(parsed)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_products"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
listProductsAPIRes := ParsedProductsResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_products_response", listProductsAPIRes, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -317,8 +316,8 @@ func (svc *PortServiceOp) GetPort(ctx context.Context, portId string) (*Port, er
portDetails := PortResponse{}
unmarshalErr := json.Unmarshal(body, &portDetails)
if unmarshalErr != nil {
expectedJSON, _ := json.Marshal(portDetails)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "get_port_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", unmarshalErr.Error()))
portDetailsRes := PortResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_port_response", portDetailsRes, unmarshalErr)
return nil, unmarshalErr
}
return &portDetails.Data, nil
Expand Down
21 changes: 10 additions & 11 deletions vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -137,8 +136,8 @@ func (svc *VXCServiceOp) BuyVXC(ctx context.Context, req *BuyVXCRequest) (*BuyVX

orderInfo := VXCOrderResponse{}
if err := json.Unmarshal(*responseBody, &orderInfo); err != nil {
expectedJSON, _ := json.Marshal(orderInfo)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(*responseBody)), slog.String("response_type", "vxc_order_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
orderRes := VXCOrderResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(*responseBody), "order_vxc_response", orderRes, err)
return nil, err
}
serviceUID := orderInfo.Data[0].TechnicalServiceUID
Expand Down Expand Up @@ -206,8 +205,8 @@ func (svc *VXCServiceOp) GetVXC(ctx context.Context, id string) (*VXC, error) {

vxcDetails := VXCResponse{}
if err = json.Unmarshal(body, &vxcDetails); err != nil {
expectedJSON, _ := json.Marshal(vxcDetails)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "get_vxc_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
vxcResponse := &VXCResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_vxc_response", vxcResponse, err)
return nil, err
}

Expand Down Expand Up @@ -309,8 +308,8 @@ func (svc *VXCServiceOp) UpdateVXC(ctx context.Context, id string, req *UpdateVX

vxcDetails := VXCResponse{}
if err = json.Unmarshal(body, &vxcDetails); err != nil {
expectedJSON, _ := json.Marshal(vxcDetails)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "get_vxc_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
getVXCRes := VXCResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "update_vxc_response", getVXCRes, err)
return nil, err
}

Expand Down Expand Up @@ -375,8 +374,8 @@ func (svc *VXCServiceOp) LookupPartnerPorts(ctx context.Context, req *LookupPart
lookupResponse := PartnerLookupResponse{}
parseErr := json.Unmarshal(body, &lookupResponse)
if parseErr != nil {
expectedJSON, _ := json.Marshal(lookupResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "partner_lookup"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", parseErr.Error()))
partnerLookupRes := PartnerLookupResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "partner_lookup_response", partnerLookupRes, parseErr)
return nil, parseErr
}

Expand Down Expand Up @@ -420,8 +419,8 @@ func (svc *VXCServiceOp) ListPartnerPorts(ctx context.Context, req *ListPartnerP
lookupResponse := PartnerLookupResponse{}
parseErr := json.Unmarshal(body, &lookupResponse)
if parseErr != nil {
expectedJSON, _ := json.Marshal(lookupResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "partner_lookup"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", parseErr.Error()))
partnerLookupResponxse := PartnerLookupResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "partner_lookup_response", partnerLookupResponxse, parseErr)
return nil, parseErr
}

Expand Down

0 comments on commit 8403d6a

Please sign in to comment.