Skip to content

Commit

Permalink
Log Spree requests and errors (vercel#650)
Browse files Browse the repository at this point in the history
  • Loading branch information
tniezg authored Jan 17, 2022
1 parent de24bd0 commit d0ef346
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 0 deletions.
7 changes: 7 additions & 0 deletions framework/spree/api/utils/create-api-fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}

Expand Down
7 changes: 7 additions & 0 deletions framework/spree/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
}

Expand Down
4 changes: 4 additions & 0 deletions framework/spree/utils/create-customized-fetch-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions framework/spree/utils/pretty-print-spree-sdk-errors.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit d0ef346

Please sign in to comment.