Skip to content

Commit d0ef346

Browse files
authored
Log Spree requests and errors (vercel#650)
1 parent de24bd0 commit d0ef346

File tree

4 files changed

+39
-0
lines changed

4 files changed

+39
-0
lines changed

framework/spree/api/utils/create-api-fetch.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import createCustomizedFetchFetcher, {
1111
} from '../../utils/create-customized-fetch-fetcher'
1212
import fetch, { Request } from 'node-fetch'
1313
import type { SpreeSdkResponseWithRawResponse } from '../../types'
14+
import prettyPrintSpreeSdkErrors from '../../utils/pretty-print-spree-sdk-errors'
1415

1516
export type CreateApiFetch = (
1617
getConfig: () => SpreeApiConfig
@@ -69,6 +70,12 @@ const createApiFetch: CreateApiFetch = (_getConfig) => {
6970
const storeResponseError = storeResponse.fail()
7071

7172
if (storeResponseError instanceof errors.SpreeError) {
73+
console.error(
74+
`Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors(
75+
storeResponse.fail()
76+
)}`
77+
)
78+
7279
throw convertSpreeErrorToGraphQlError(storeResponseError)
7380
}
7481

framework/spree/fetcher.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import createCustomizedFetchFetcher, {
1616
} from './utils/create-customized-fetch-fetcher'
1717
import ensureFreshUserAccessToken from './utils/tokens/ensure-fresh-user-access-token'
1818
import RefreshTokenError from './errors/RefreshTokenError'
19+
import prettyPrintSpreeSdkErrors from './utils/pretty-print-spree-sdk-errors'
1920

2021
const client = makeClient({
2122
host: requireConfigValue('apiHost') as string,
@@ -107,6 +108,12 @@ const fetcher: Fetcher<GraphQLFetcherResult<SpreeSdkResponse>> = async (
107108
}
108109

109110
if (storeResponseError instanceof errors.SpreeError) {
111+
console.error(
112+
`Request to spree resulted in an error:\n\n${prettyPrintSpreeSdkErrors(
113+
storeResponse.fail()
114+
)}`
115+
)
116+
110117
throw convertSpreeErrorToGraphQlError(storeResponseError)
111118
}
112119

framework/spree/utils/create-customized-fetch-fetcher.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (
5050
)
5151

5252
try {
53+
console.info(
54+
`Calling the Spree API: ${request.method} ${request.url}`
55+
)
56+
5357
const response: Response = await fetch(request)
5458
const responseContentType = response.headers.get('content-type')
5559
let data
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { errors } from '@spree/storefront-api-v2-sdk'
2+
3+
const prettyPrintSpreeSdkErrors = (error: errors.SpreeSDKError): string => {
4+
let prettyOutput = `Name: ${error.name}\nMessage: ${error.message}`
5+
6+
if (error instanceof errors.BasicSpreeError) {
7+
prettyOutput += `\nSpree summary: ${error.summary}`
8+
9+
if (error instanceof errors.ExpandedSpreeError) {
10+
prettyOutput += `\nSpree validation errors:\n${JSON.stringify(
11+
error.errors,
12+
null,
13+
2
14+
)}`
15+
}
16+
}
17+
18+
return prettyOutput
19+
}
20+
21+
export default prettyPrintSpreeSdkErrors

0 commit comments

Comments
 (0)