Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: logging for json unmarshaling errors in Go SDK #79

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()),
)
}

Comment on lines +313 to +322
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think a better way to approach this would be to add a config option to the logger that would enable logging of all request JSON optionally.

in client.go we have a Do() method that logs every request already, I think what would be good is to add some extra client config to log the response body as part of the existing request logs. I've added the section that does that logging here.

c.Logger.DebugContext(ctx, "completed API request",
		slog.Duration("duration", reqTime),
		slog.Int("status_code", resp.StatusCode),
		slog.String("path", req.URL.EscapedPath()),
		slog.String("api_host", c.BaseURL.Host),
		slog.String("method", req.Method),
		slog.String("trace_id", resp.Header.Get(headerTraceId)),
	)

type AuthInfo struct {
Expiration time.Time
AccessToken string
Expand Down
4 changes: 4 additions & 0 deletions location.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ func (svc *LocationServiceOp) ListLocations(ctx context.Context) ([]*Location, e

unmarshalErr := json.Unmarshal(body, locationResponse)
if unmarshalErr != nil {
locationRes := LocationResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_locations_response", locationRes, unmarshalErr)
return nil, unmarshalErr
}

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

if unmarshalErr != nil {
countryResp := CountryResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_countries_response", countryResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down
6 changes: 6 additions & 0 deletions managed_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ func (svc *ManagedAccountServiceOp) ListManagedAccounts(ctx context.Context) ([]
var apiResponse *ManagedAccountListAPIResponse

if err := json.Unmarshal(body, &apiResponse); err != nil {
managedAccountsAPIRes := ManagedAccountListAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_managed_accounts_response", managedAccountsAPIRes, err)
return nil, err
}

Expand All @@ -96,6 +98,8 @@ func (svc *ManagedAccountServiceOp) CreateManagedAccount(ctx context.Context, re
}
var createManagedAccountResponse *ManagedAccountAPIResponse
if err := json.Unmarshal(body, &createManagedAccountResponse); err != nil {
managedAccountResponse := ManagedAccountAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "create_managed_account_response", managedAccountResponse, err)
return nil, err
}
return createManagedAccountResponse.Data, nil
Expand All @@ -118,6 +122,8 @@ func (svc *ManagedAccountServiceOp) UpdateManagedAccount(ctx context.Context, co
}
var updateManagedAccountResponse *ManagedAccountAPIResponse
if err := json.Unmarshal(body, &updateManagedAccountResponse); err != nil {
updateManagedAccountResponse := ManagedAccountAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "update_managed_account_response", updateManagedAccountResponse, err)
return nil, err
}
return updateManagedAccountResponse.Data, nil
Expand Down
10 changes: 10 additions & 0 deletions mcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ func (svc *MCRServiceOp) BuyMCR(ctx context.Context, req *BuyMCRRequest) (*BuyMC
unmarshalErr := json.Unmarshal(*body, &orderInfo)

if unmarshalErr != nil {
orderResponse := MCROrderResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(*body), "buy_mcr_response", orderResponse, unmarshalErr)
return nil, unmarshalErr
}

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

if unmarshalErr != nil {
mcrResp := MCRResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_mcr_response", mcrResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -286,6 +290,8 @@ func (svc *MCRServiceOp) CreatePrefixFilterList(ctx context.Context, req *Create
createRes := &APIMCRPrefixFilterListResponse{}
unmarshalErr := json.Unmarshal(body, createRes)
if unmarshalErr != nil {
createResp := APIMCRPrefixFilterListResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "create_mcr_prefix_filter_list_response", createResp, unmarshalErr)
return nil, unmarshalErr
}

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

if unmarshalErr != nil {
listPrefixFilterListResp := ListMCRPrefixFilterListResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_mcr_prefix_filter_list_response", listPrefixFilterListResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -358,6 +366,8 @@ func (svc *MCRServiceOp) GetMCRPrefixFilterList(ctx context.Context, mcrID strin
apiPrefixFilterList := &APIMCRPrefixFilterListResponse{}
unmarshalErr := json.Unmarshal(body, apiPrefixFilterList)
if unmarshalErr != nil {
apiPrefixFilterListResp := APIMCRPrefixFilterListResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_mcr_prefix_filter_list_response", apiPrefixFilterListResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down
8 changes: 8 additions & 0 deletions mve.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ func (svc *MVEServiceOp) BuyMVE(ctx context.Context, req *BuyMVERequest) (*BuyMV
orderInfo := MVEOrderResponse{}

if err := json.Unmarshal(*resp, &orderInfo); err != nil {
mveResp := MVEOrderResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(*resp), "buy_mve_response", mveResp, err)
return nil, err
}

Expand Down Expand Up @@ -198,6 +200,8 @@ func (svc *MVEServiceOp) GetMVE(ctx context.Context, mveId string) (*MVE, error)
}
mveResp := MVEResponse{}
if err := json.Unmarshal(body, &mveResp); err != nil {
getMVERes := MVEResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_mve_response", getMVERes, err)
return nil, err
}

Expand Down Expand Up @@ -290,6 +294,8 @@ func (svc *MVEServiceOp) ListMVEImages(ctx context.Context) ([]*MVEImage, error)
}
imageResp := MVEImageAPIResponse{}
if err := json.Unmarshal(body, &imageResp); err != nil {
listImageRes := MVEImageAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_mve_images_response", listImageRes, err)
return nil, err
}
return imageResp.Data.Images, nil
Expand All @@ -313,6 +319,8 @@ func (svc *MVEServiceOp) ListAvailableMVESizes(ctx context.Context) ([]*MVESize,
}
sizeResp := MVESizeAPIResponse{}
if err := json.Unmarshal(body, &sizeResp); err != nil {
listSizesResp := MVESizeAPIResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_mve_sizes_response", listSizesResp, err)
return nil, err
}
return sizeResp.Data, nil
Expand Down
2 changes: 2 additions & 0 deletions partner.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ func (svc *PartnerServiceOp) ListPartnerMegaports(ctx context.Context) ([]*Partn
partnerMegaportResponse := PartnerMegaportResponse{}
unmarshalErr := json.Unmarshal(body, &partnerMegaportResponse)
if unmarshalErr != nil {
partnerResp := PartnerMegaportResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_partner_megaports_response", partnerResp, unmarshalErr)
return nil, unmarshalErr
}

Expand Down
6 changes: 6 additions & 0 deletions port.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ func (svc *PortServiceOp) BuyPort(ctx context.Context, req *BuyPortRequest) (*Bu
orderInfo := PortOrderResponse{}
unmarshalErr := json.Unmarshal(*responseBody, &orderInfo)
if unmarshalErr != nil {
portOrderResp := PortOrderResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(*responseBody), "buy_port_response", portOrderResp, unmarshalErr)
return nil, unmarshalErr
}

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

if unmarshalErr != nil {
listProductsAPIRes := ParsedProductsResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "list_products_response", listProductsAPIRes, unmarshalErr)
return nil, unmarshalErr
}

Expand Down Expand Up @@ -312,6 +316,8 @@ func (svc *PortServiceOp) GetPort(ctx context.Context, portId string) (*Port, er
portDetails := PortResponse{}
unmarshalErr := json.Unmarshal(body, &portDetails)
if unmarshalErr != nil {
portDetailsRes := PortResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_port_response", portDetailsRes, unmarshalErr)
return nil, unmarshalErr
}
return &portDetails.Data, nil
Expand Down
6 changes: 6 additions & 0 deletions service_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ func (svc *ServiceKeyServiceOp) CreateServiceKey(ctx context.Context, req *Creat
}
var createServiceKeyAPIResponse CreateServiceKeyAPIResponse
if err = json.Unmarshal(body, &createServiceKeyAPIResponse); err != nil {
expectedJSON, _ := json.Marshal(createServiceKeyAPIResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "create_service_key_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
return nil, err
}
toReturn := &CreateServiceKeyResponse{
Expand Down Expand Up @@ -204,6 +206,8 @@ func (svc *ServiceKeyServiceOp) ListServiceKeys(ctx context.Context, req *ListSe
}
listServiceKeysAPIResponse := ListServiceKeysAPIResponse{}
if err = json.Unmarshal(body, &listServiceKeysAPIResponse); err != nil {
expectedJSON, _ := json.Marshal(listServiceKeysAPIResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "list_service_keys_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
return nil, err
}
toReturn := &ListServiceKeysResponse{}
Expand Down Expand Up @@ -250,6 +254,8 @@ func (svc *ServiceKeyServiceOp) GetServiceKey(ctx context.Context, keyId string)
}
parsedAPIResponse := GetServiceKeyAPIResponse{}
if err = json.Unmarshal(body, &parsedAPIResponse); err != nil {
expectedJSON, _ := json.Marshal(parsedAPIResponse)
svc.Client.Logger.DebugContext(ctx, "error unmarshaling response body", slog.String("response_body", string(body)), slog.String("response_type", "get_service_key_response"), slog.String("expected_json_schema", string(expectedJSON)), slog.String("error", err.Error()))
return nil, err
}
return parsedAPIResponse.Data, nil
Expand Down
12 changes: 10 additions & 2 deletions vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ func (svc *VXCServiceOp) BuyVXC(ctx context.Context, req *BuyVXCRequest) (*BuyVX

orderInfo := VXCOrderResponse{}
if err := json.Unmarshal(*responseBody, &orderInfo); err != nil {
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,6 +208,8 @@ func (svc *VXCServiceOp) GetVXC(ctx context.Context, id string) (*VXC, error) {

vxcDetails := VXCResponse{}
if err = json.Unmarshal(body, &vxcDetails); err != nil {
vxcResponse := &VXCResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "get_vxc_response", vxcResponse, err)
return nil, err
}

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

vxcDetails := VXCResponse{}
if err = json.Unmarshal(body, &vxcDetails); err != nil {
getVXCRes := VXCResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "update_vxc_response", getVXCRes, err)
return nil, err
}

Expand Down Expand Up @@ -390,8 +396,9 @@ func (svc *VXCServiceOp) LookupPartnerPorts(ctx context.Context, req *LookupPart

lookupResponse := PartnerLookupResponse{}
parseErr := json.Unmarshal(body, &lookupResponse)

if parseErr != nil {
partnerLookupRes := PartnerLookupResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "partner_lookup_response", partnerLookupRes, parseErr)
return nil, parseErr
}

Expand Down Expand Up @@ -434,8 +441,9 @@ func (svc *VXCServiceOp) ListPartnerPorts(ctx context.Context, req *ListPartnerP

lookupResponse := PartnerLookupResponse{}
parseErr := json.Unmarshal(body, &lookupResponse)

if parseErr != nil {
partnerLookupResponxse := PartnerLookupResponse{}
svc.Client.LogJSONUnmarshalError(ctx, string(body), "partner_lookup_response", partnerLookupResponxse, parseErr)
return nil, parseErr
}

Expand Down