forked from crowetic/commerce
Log Spree requests and errors (#650)
This commit is contained in:
parent
de24bd041c
commit
d0ef346189
@ -11,6 +11,7 @@ import createCustomizedFetchFetcher, {
|
|||||||
} from '../../utils/create-customized-fetch-fetcher'
|
} from '../../utils/create-customized-fetch-fetcher'
|
||||||
import fetch, { Request } from 'node-fetch'
|
import fetch, { Request } from 'node-fetch'
|
||||||
import type { SpreeSdkResponseWithRawResponse } from '../../types'
|
import type { SpreeSdkResponseWithRawResponse } from '../../types'
|
||||||
|
import prettyPrintSpreeSdkErrors from '../../utils/pretty-print-spree-sdk-errors'
|
||||||
|
|
||||||
export type CreateApiFetch = (
|
export type CreateApiFetch = (
|
||||||
getConfig: () => SpreeApiConfig
|
getConfig: () => SpreeApiConfig
|
||||||
@ -69,6 +70,12 @@ const createApiFetch: CreateApiFetch = (_getConfig) => {
|
|||||||
const storeResponseError = storeResponse.fail()
|
const storeResponseError = storeResponse.fail()
|
||||||
|
|
||||||
if (storeResponseError instanceof errors.SpreeError) {
|
if (storeResponseError instanceof errors.SpreeError) {
|
||||||
|
console.error(
|
||||||
|
`Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors(
|
||||||
|
storeResponse.fail()
|
||||||
|
)}`
|
||||||
|
)
|
||||||
|
|
||||||
throw convertSpreeErrorToGraphQlError(storeResponseError)
|
throw convertSpreeErrorToGraphQlError(storeResponseError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -16,6 +16,7 @@ import createCustomizedFetchFetcher, {
|
|||||||
} from './utils/create-customized-fetch-fetcher'
|
} from './utils/create-customized-fetch-fetcher'
|
||||||
import ensureFreshUserAccessToken from './utils/tokens/ensure-fresh-user-access-token'
|
import ensureFreshUserAccessToken from './utils/tokens/ensure-fresh-user-access-token'
|
||||||
import RefreshTokenError from './errors/RefreshTokenError'
|
import RefreshTokenError from './errors/RefreshTokenError'
|
||||||
|
import prettyPrintSpreeSdkErrors from './utils/pretty-print-spree-sdk-errors'
|
||||||
|
|
||||||
const client = makeClient({
|
const client = makeClient({
|
||||||
host: requireConfigValue('apiHost') as string,
|
host: requireConfigValue('apiHost') as string,
|
||||||
@ -107,6 +108,12 @@ const fetcher: Fetcher<GraphQLFetcherResult<SpreeSdkResponse>> = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (storeResponseError instanceof errors.SpreeError) {
|
if (storeResponseError instanceof errors.SpreeError) {
|
||||||
|
console.error(
|
||||||
|
`Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors(
|
||||||
|
storeResponse.fail()
|
||||||
|
)}`
|
||||||
|
)
|
||||||
|
|
||||||
throw convertSpreeErrorToGraphQlError(storeResponseError)
|
throw convertSpreeErrorToGraphQlError(storeResponseError)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,6 +50,10 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (
|
|||||||
)
|
)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
console.info(
|
||||||
|
`Calling the Spree API: ${request.method} ${request.url}`
|
||||||
|
)
|
||||||
|
|
||||||
const response: Response = await fetch(request)
|
const response: Response = await fetch(request)
|
||||||
const responseContentType = response.headers.get('content-type')
|
const responseContentType = response.headers.get('content-type')
|
||||||
let data
|
let data
|
||||||
|
21
framework/spree/utils/pretty-print-spree-sdk-errors.ts
Normal file
21
framework/spree/utils/pretty-print-spree-sdk-errors.ts
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user