saleor: arrange the error codes

This commit is contained in:
Zaiste 2021-05-12 12:08:12 +02:00
parent f4b66f2132
commit 9ca6c38755
No known key found for this signature in database
GPG Key ID: 15DF7EBC7F2FFE35

View File

@ -1,35 +1,21 @@
import { ValidationError } from '@commerce/utils/errors'
import {
CheckoutErrorCode,
CheckoutUserError,
CustomerErrorCode,
CustomerUserError,
} from '../schema'
import { CheckoutError, CheckoutErrorCode, AppError, AccountError, AccountErrorCode } from '../schema'
export type UserErrors = Array<CheckoutUserError | CustomerUserError>
export type UserErrors = Array<CheckoutError | AccountError | AppError>
export type UserErrorCode =
| CustomerErrorCode
| CheckoutErrorCode
| AccountErrorCode
| 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),
message: message || "",
})),
})
}