From d0ef3461899bf9557ee825d838f10839d1d2c0ef Mon Sep 17 00:00:00 2001 From: Tomasz Niezgoda <1410097+tniezg@users.noreply.github.com> Date: Mon, 17 Jan 2022 13:12:55 +0100 Subject: [PATCH] Log Spree requests and errors (#650) --- framework/spree/api/utils/create-api-fetch.ts | 7 +++++++ framework/spree/fetcher.ts | 7 +++++++ .../utils/create-customized-fetch-fetcher.ts | 4 ++++ .../utils/pretty-print-spree-sdk-errors.ts | 21 +++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 framework/spree/utils/pretty-print-spree-sdk-errors.ts diff --git a/framework/spree/api/utils/create-api-fetch.ts b/framework/spree/api/utils/create-api-fetch.ts index 0c7d51b0b..f6810592b 100644 --- a/framework/spree/api/utils/create-api-fetch.ts +++ b/framework/spree/api/utils/create-api-fetch.ts @@ -11,6 +11,7 @@ import createCustomizedFetchFetcher, { } from '../../utils/create-customized-fetch-fetcher' import fetch, { Request } from 'node-fetch' import type { SpreeSdkResponseWithRawResponse } from '../../types' +import prettyPrintSpreeSdkErrors from '../../utils/pretty-print-spree-sdk-errors' export type CreateApiFetch = ( getConfig: () => SpreeApiConfig @@ -69,6 +70,12 @@ const createApiFetch: CreateApiFetch = (_getConfig) => { const storeResponseError = storeResponse.fail() if (storeResponseError instanceof errors.SpreeError) { + console.error( + `Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors( + storeResponse.fail() + )}` + ) + throw convertSpreeErrorToGraphQlError(storeResponseError) } diff --git a/framework/spree/fetcher.ts b/framework/spree/fetcher.ts index c9505e4c9..379cd3cb7 100644 --- a/framework/spree/fetcher.ts +++ b/framework/spree/fetcher.ts @@ -16,6 +16,7 @@ import createCustomizedFetchFetcher, { } from './utils/create-customized-fetch-fetcher' import ensureFreshUserAccessToken from './utils/tokens/ensure-fresh-user-access-token' import RefreshTokenError from './errors/RefreshTokenError' +import prettyPrintSpreeSdkErrors from './utils/pretty-print-spree-sdk-errors' const client = makeClient({ host: requireConfigValue('apiHost') as string, @@ -107,6 +108,12 @@ const fetcher: Fetcher> = async ( } if (storeResponseError instanceof errors.SpreeError) { + console.error( + `Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors( + storeResponse.fail() + )}` + ) + throw convertSpreeErrorToGraphQlError(storeResponseError) } diff --git a/framework/spree/utils/create-customized-fetch-fetcher.ts b/framework/spree/utils/create-customized-fetch-fetcher.ts index 1c10b19e9..f0821c19a 100644 --- a/framework/spree/utils/create-customized-fetch-fetcher.ts +++ b/framework/spree/utils/create-customized-fetch-fetcher.ts @@ -50,6 +50,10 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = ( ) try { + console.info( + `Calling the Spree API: ${request.method} ${request.url}` + ) + const response: Response = await fetch(request) const responseContentType = response.headers.get('content-type') let data diff --git a/framework/spree/utils/pretty-print-spree-sdk-errors.ts b/framework/spree/utils/pretty-print-spree-sdk-errors.ts new file mode 100644 index 000000000..791742046 --- /dev/null +++ b/framework/spree/utils/pretty-print-spree-sdk-errors.ts @@ -0,0 +1,21 @@ +import { errors } from '@spree/storefront-api-v2-sdk' + +const prettyPrintSpreeSdkErrors = (error: errors.SpreeSDKError): string => { + let prettyOutput = `Name: ${error.name}\nMessage: ${error.message}` + + if (error instanceof errors.BasicSpreeError) { + prettyOutput += `\nSpree summary: ${error.summary}` + + if (error instanceof errors.ExpandedSpreeError) { + prettyOutput += `\nSpree validation errors:\n${JSON.stringify( + error.errors, + null, + 2 + )}` + } + } + + return prettyOutput +} + +export default prettyPrintSpreeSdkErrors