commerce/framework/saleor/utils/throw-user-errors.ts
2021-06-09 17:02:12 +02:00

25 lines
637 B
TypeScript

import { ValidationError } from '@commerce/utils/errors'
import { CheckoutError, CheckoutErrorCode, AppError, AccountError, AccountErrorCode } from '../schema'
export type UserErrors = Array<CheckoutError | AccountError | AppError>
export type UserErrorCode =
| CheckoutErrorCode
| AccountErrorCode
| null
| undefined
export const throwUserErrors = (errors?: UserErrors) => {
if (errors && errors.length) {
throw new ValidationError({
errors: errors.map(({ code, message }) => ({
code: code ?? 'validation_error',
message: message || "",
})),
})
}
}
export default throwUserErrors