mirror of
https://github.com/vercel/commerce.git
synced 2025-07-25 19:21:23 +00:00
Add basic Spree framework structure
This commit is contained in:
46
framework/spree/utils/convertSpreeErrorToGraphQlError.ts
Normal file
46
framework/spree/utils/convertSpreeErrorToGraphQlError.ts
Normal file
@@ -0,0 +1,46 @@
|
||||
import { FetcherError } from '@commerce/utils/errors'
|
||||
import { errors } from '@spree/storefront-api-v2-sdk'
|
||||
|
||||
const convertSpreeErrorToGraphQlError = (
|
||||
error: errors.SpreeError
|
||||
): FetcherError => {
|
||||
if (error instanceof errors.ExpandedSpreeError) {
|
||||
// Assuming error.errors[key] is a list of strings.
|
||||
|
||||
if ('base' in error.errors) {
|
||||
const baseErrorMessage = error.errors.base as unknown as string
|
||||
|
||||
return new FetcherError({
|
||||
status: error.serverResponse.status,
|
||||
message: baseErrorMessage,
|
||||
})
|
||||
}
|
||||
|
||||
const fetcherErrors = Object.keys(error.errors).map((sdkErrorKey) => {
|
||||
const errors = error.errors[sdkErrorKey] as string[]
|
||||
|
||||
return {
|
||||
message: `${sdkErrorKey} ${errors.join(', ')}`,
|
||||
}
|
||||
})
|
||||
|
||||
return new FetcherError({
|
||||
status: error.serverResponse.status,
|
||||
errors: fetcherErrors,
|
||||
})
|
||||
}
|
||||
|
||||
if (error instanceof errors.BasicSpreeError) {
|
||||
return new FetcherError({
|
||||
status: error.serverResponse.status,
|
||||
message: error.summary,
|
||||
})
|
||||
}
|
||||
|
||||
return new FetcherError({
|
||||
status: error.serverResponse.status,
|
||||
message: error.message,
|
||||
})
|
||||
}
|
||||
|
||||
export default convertSpreeErrorToGraphQlError
|
Reference in New Issue
Block a user