mirror of
https://github.com/vercel/commerce.git
synced 2025-06-20 06:01:21 +00:00
saleor: arrange the error codes
This commit is contained in:
parent
f4b66f2132
commit
9ca6c38755
@ -1,35 +1,21 @@
|
|||||||
import { ValidationError } from '@commerce/utils/errors'
|
import { ValidationError } from '@commerce/utils/errors'
|
||||||
|
|
||||||
import {
|
import { CheckoutError, CheckoutErrorCode, AppError, AccountError, AccountErrorCode } from '../schema'
|
||||||
CheckoutErrorCode,
|
|
||||||
CheckoutUserError,
|
|
||||||
CustomerErrorCode,
|
|
||||||
CustomerUserError,
|
|
||||||
} from '../schema'
|
|
||||||
|
|
||||||
export type UserErrors = Array<CheckoutUserError | CustomerUserError>
|
export type UserErrors = Array<CheckoutError | AccountError | AppError>
|
||||||
|
|
||||||
export type UserErrorCode =
|
export type UserErrorCode =
|
||||||
| CustomerErrorCode
|
|
||||||
| CheckoutErrorCode
|
| CheckoutErrorCode
|
||||||
|
| AccountErrorCode
|
||||||
| null
|
| null
|
||||||
| undefined
|
| 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) => {
|
export const throwUserErrors = (errors?: UserErrors) => {
|
||||||
if (errors && errors.length) {
|
if (errors && errors.length) {
|
||||||
throw new ValidationError({
|
throw new ValidationError({
|
||||||
errors: errors.map(({ code, message }) => ({
|
errors: errors.map(({ code, message }) => ({
|
||||||
code: code ?? 'validation_error',
|
code: code ?? 'validation_error',
|
||||||
message: getCustomMessage(code, message),
|
message: message || "",
|
||||||
})),
|
})),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user