UNPKG

@apite/magento2-utility

Version:

Shopgate WebCheckout utility for Magento 2 extensions

129 lines (112 loc) 3.56 kB
'use strict' const { formatAxiosRequest } = require('../services/logDecorator')() class CartError extends Error { constructor (message = '', code = 'EUNKNOWN', translated = true, messageParams = undefined) { super() this.message = 'Error' this.code = 'ECART' /** * @type {ApiteM2Cart.SGCartError[]} */ this.errors = [{ code, message, translated, messageParams }] } } class CartNotFoundError extends CartError { constructor (message) { super(message, 'ECARTNOTFOUND', true) } } class ProductNotFoundError extends CartError { constructor (message) { super(message, 'ENOTFOUND', true) } } class ProductNotFoundErrorWithParams extends CartError { /** * @param {?{productId: string}} params * @param {?string} message */ constructor (params, message = undefined) { super(message || 'ApiteM2Cart.cart.product-not-found', 'ENOTFOUND', false, params) } } class ProductStockError extends CartError { constructor (message) { super(message, 'ESTOCKISSUE', true) } } class PromoNotFoundError extends CartError { constructor (message) { super(message, 'EINVALIDCOUPON', true) } } class TranslatableError extends Error { constructor (message, code, params = {}) { super() this.message = message this.code = code this.messageParams = params } } class InvalidCredentialsError extends TranslatableError { constructor (message) { super(message, 'EBADCREDENTIALS') } } /** * Code handled by theme, guest trying to call customer pipelines */ class UnauthorizedError extends TranslatableError { constructor () { super('', 'EACCESS') } } class UnknownError extends TranslatableError { constructor () { super('ApiteM2Cart.app.message-default', 'EUNKNOWN', false) } } /** * A state of the app that requires the current App user to be logged out. * Our frontend subscriber handles this error. Cart ext also has a similar * catcher, so it's possible this error may not be fired. */ class ContextDeSyncError extends TranslatableError { constructor () { super('ApiteM2Cart.app.not-in-sync', 'EDESYNC', false) } } class GraphQLError extends Error { /** * @param {AxiosResponse} axiosResponse */ constructor (axiosResponse) { const errorList = axiosResponse.data.errors super(errorList.map(e => e.message).join(',')) this.name = 'GraphQLError' this.config = axiosResponse.config this.data = axiosResponse.data this.errorList = errorList } /** * @return {FormattedAxiosRequest & errorList: Array<{message: string, path: string[]}>}} */ getFormattedError () { return { ...formatAxiosRequest(this.config), errorList: this.errorList.map(e => { return { ...e, locations: JSON.stringify(e.locations), path: JSON.stringify(e.path), extensions: JSON.stringify(e.extensions) } }) } } /** * Checks if any error in the errorList has a path that matches the given pattern. * * @param {string} pattern - The pattern to match against the error paths. * @return {boolean} - Returns true if a matching path is found, otherwise false. */ hasPath (pattern) { const regex = new RegExp(pattern) return this.errorList.some(error => error.path && error.path.some(path => regex.test(path))) } } module.exports = { CartError, ContextDeSyncError, InvalidCredentialsError, GraphQLError, PromoNotFoundError, ProductNotFoundError, ProductNotFoundErrorWithParams, ProductStockError, UnauthorizedError, UnknownError, CartNotFoundError }