Skip to content

Commit

Permalink
feat: logging for json unmarshaling errors
Browse files Browse the repository at this point in the history
  • Loading branch information
MegaportPhilipBrowne committed Oct 1, 2024
1 parent 5997df4 commit 098d181
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 2 deletions.
5 changes: 5 additions & 0 deletions location.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"

"github.com/lithammer/fuzzysearch/fuzzy"
Expand Down Expand Up @@ -153,6 +154,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()))
return nil, unmarshalErr
}

Expand Down Expand Up @@ -232,6 +235,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()))
return nil, unmarshalErr
}

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

type ManagedAccountService interface {
Expand Down Expand Up @@ -73,6 +74,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()))
return nil, err
}

Expand All @@ -96,6 +99,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()))
return nil, err
}
return createManagedAccountResponse.Data, nil
Expand All @@ -118,6 +123,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()))
return nil, err
}
return updateManagedAccountResponse.Data, nil
Expand Down
11 changes: 11 additions & 0 deletions mcr.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"slices"
"strconv"
"time"
Expand Down Expand Up @@ -145,6 +146,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()))
return nil, unmarshalErr
}

Expand Down Expand Up @@ -255,6 +258,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()))
return nil, unmarshalErr
}

Expand Down Expand Up @@ -286,6 +291,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()))
return nil, unmarshalErr
}

Expand Down Expand Up @@ -328,6 +335,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()))
return nil, unmarshalErr
}

Expand Down Expand Up @@ -358,6 +367,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()))
return nil, unmarshalErr
}

Expand Down
9 changes: 9 additions & 0 deletions mve.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -104,6 +105,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()))
return nil, err
}

Expand Down Expand Up @@ -198,6 +201,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()))
return nil, err
}

Expand Down Expand Up @@ -290,6 +295,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()))
return nil, err
}
return imageResp.Data.Images, nil
Expand All @@ -313,6 +320,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()))
return nil, err
}
return sizeResp.Data, nil
Expand Down
3 changes: 3 additions & 0 deletions partner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"io"
"log/slog"
"net/http"

"github.com/lithammer/fuzzysearch/fuzzy"
Expand Down Expand Up @@ -58,6 +59,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()))
return nil, unmarshalErr
}

Expand Down
7 changes: 7 additions & 0 deletions port.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"slices"
Expand Down Expand Up @@ -146,6 +147,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()))
return nil, unmarshalErr
}

Expand Down Expand Up @@ -258,6 +261,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()))
return nil, unmarshalErr
}

Expand Down Expand Up @@ -312,6 +317,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()))
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
13 changes: 11 additions & 2 deletions vxc.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"slices"
"strings"
Expand Down Expand Up @@ -136,6 +137,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()))
return nil, err
}
serviceUID := orderInfo.Data[0].TechnicalServiceUID
Expand Down Expand Up @@ -203,6 +206,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()))
return nil, err
}

Expand Down Expand Up @@ -304,6 +309,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()))
return nil, err
}

Expand Down Expand Up @@ -367,8 +374,9 @@ 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()))
return nil, parseErr
}

Expand Down Expand Up @@ -411,8 +419,9 @@ 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()))
return nil, parseErr
}

Expand Down

0 comments on commit 098d181

Please sign in to comment.