4
0
forked from crowetic/commerce

Log Spree requests and errors (#650)

This commit is contained in:
Tomasz Niezgoda 2022-01-17 13:12:55 +01:00 committed by GitHub
parent de24bd041c
commit d0ef346189
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 39 additions and 0 deletions

View File

@ -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)
}

View File

@ -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<GraphQLFetcherResult<SpreeSdkResponse>> = async (
}
if (storeResponseError instanceof errors.SpreeError) {
console.error(
`Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors(
storeResponse.fail()
)}`
)
throw convertSpreeErrorToGraphQlError(storeResponseError)
}

View File

@ -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

View 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