UNPKG

@shopify/app-bridge-core

Version:

**[Join our team and work on libraries like this one.](https://www.shopify.ca/careers)**

103 lines (100 loc) 3.78 kB
import { Group } from '../types.js'; import { actionWrapper, findMatchInEnum } from '../helper.js'; // Errors triggered in response to an action var Action; (function (Action) { Action["INVALID_ACTION"] = "APP::ERROR::INVALID_ACTION"; Action["INVALID_ACTION_TYPE"] = "APP::ERROR::INVALID_ACTION_TYPE"; Action["INVALID_PAYLOAD"] = "APP::ERROR::INVALID_PAYLOAD"; Action["INVALID_OPTIONS"] = "APP::ERROR::INVALID_OPTIONS"; Action["UNEXPECTED_ACTION"] = "APP::ERROR::UNEXPECTED_ACTION"; Action["PERSISTENCE"] = "APP::ERROR::PERSISTENCE"; Action["UNSUPPORTED_OPERATION"] = "APP::ERROR::UNSUPPORTED_OPERATION"; Action["NETWORK"] = "APP::ERROR::NETWORK"; Action["PERMISSION"] = "APP::ERROR::PERMISSION"; Action["FAILED_AUTHENTICATION"] = "APP::ERROR::FAILED_AUTHENTICATION"; Action["INVALID_ORIGIN"] = "APP::ERROR::INVALID_ORIGIN"; })(Action || (Action = {})); // Errors thrown in response to app setup var AppActionType; (function (AppActionType) { AppActionType["INVALID_CONFIG"] = "APP::ERROR::INVALID_CONFIG"; AppActionType["MISSING_CONFIG"] = "APP::APP_ERROR::MISSING_CONFIG"; AppActionType["MISSING_APP_BRIDGE_MIDDLEWARE"] = "APP::APP_ERROR::MISSING_APP_BRIDGE_MIDDLEWARE"; AppActionType["WINDOW_UNDEFINED"] = "APP::APP_ERROR::WINDOW_UNDEFINED"; AppActionType["REDUX_REINSTANTIATED"] = "APP::APP_ERROR::REDUX_REINSTANTIATED"; AppActionType["MISSING_LOCAL_ORIGIN"] = "APP::APP_ERROR::MISSING_LOCAL_ORIGIN"; AppActionType["MISSING_HOST_PROVIDER"] = "APP::APP_ERROR::MISSING_HOST_PROVIDER"; AppActionType["MISSING_ROUTER_CONTEXT"] = "APP::APP_ERROR::MISSING_ROUTER_CONTEXT"; AppActionType["MISSING_HISTORY_BLOCK"] = "APP::APP_ERROR::MISSING_HISTORY_BLOCK"; })(AppActionType || (AppActionType = {})); class AppBridgeError extends Error { message; name; stack; action; type; constructor(message) { super(message); this.name = 'AppBridgeError'; this.message = message; Object.setPrototypeOf(this, AppBridgeError.prototype); if (typeof Error.captureStackTrace === 'function') { Error.captureStackTrace(this, this.constructor); } else { this.stack = new Error(this.message).stack; } } } function fromAction(message, type, action) { const errorMessage = message ? `${type}: ${message}` : type; const error = new AppBridgeError(errorMessage); error.action = action; error.type = type; return error; } function invalidOriginAction(message) { return actionWrapper({ group: Group.Error, payload: { message, type: Action.INVALID_ORIGIN, }, type: Action.INVALID_ORIGIN, }); } function throwError(...args) { const type = args[0]; let message; let action; if (typeof args[1] === 'string') { message = args[1]; } else { action = args[1]; message = args[2] || ''; } throw fromAction(message, type, action); } function isErrorEventName(eventName) { const match = findMatchInEnum(Action, eventName); return typeof match === 'string'; } function errorActionWrapperWithId(type, action, message) { const castPayload = action.payload; return actionWrapper({ type, group: Group.Error, payload: { action, message, type, id: castPayload && castPayload.id ? castPayload.id : undefined, }, }); } function permissionAction(action, message) { return errorActionWrapperWithId(Action.PERMISSION, action, message || 'Action is not permitted'); } export { Action, AppActionType, AppBridgeError, fromAction, invalidOriginAction, isErrorEventName, permissionAction, throwError };