mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
Initial work, copied from the Shopify provider
This commit is contained in:
38
framework/saleor/utils/throw-user-errors.ts
Normal file
38
framework/saleor/utils/throw-user-errors.ts
Normal file
@@ -0,0 +1,38 @@
|
||||
import { ValidationError } from '@commerce/utils/errors'
|
||||
|
||||
import {
|
||||
CheckoutErrorCode,
|
||||
CheckoutUserError,
|
||||
CustomerErrorCode,
|
||||
CustomerUserError,
|
||||
} from '../schema'
|
||||
|
||||
export type UserErrors = Array<CheckoutUserError | CustomerUserError>
|
||||
|
||||
export type UserErrorCode =
|
||||
| CustomerErrorCode
|
||||
| CheckoutErrorCode
|
||||
| null
|
||||
| undefined
|
||||
|
||||
const getCustomMessage = (code: UserErrorCode, message: string) => {
|
||||
switch (code) {
|
||||
case 'UNIDENTIFIED_CUSTOMER':
|
||||
message = 'Cannot find an account that matches the provided credentials'
|
||||
break
|
||||
}
|
||||
return message
|
||||
}
|
||||
|
||||
export const throwUserErrors = (errors?: UserErrors) => {
|
||||
if (errors && errors.length) {
|
||||
throw new ValidationError({
|
||||
errors: errors.map(({ code, message }) => ({
|
||||
code: code ?? 'validation_error',
|
||||
message: getCustomMessage(code, message),
|
||||
})),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
export default throwUserErrors
|
Reference in New Issue
Block a user