UNPKG

@shopify/shop-minis-react

Version:

React component library for Shopify Shop Minis with Tailwind CSS v4 support (source-only, requires TypeScript)

87 lines (74 loc) 1.92 kB
interface MiniErrorSource { component?: string hook?: string func?: string callback?: string } interface MiniErrorParams extends MiniErrorSource { message: string } // Base class for all Mini errors // We can give richer error messages by using this throughout the SDK // In future we could add instrumentation to track these errors too export class MiniError extends Error { constructor({ component, hook, func, callback, message = 'Unknown Error', }: MiniErrorParams) { let formattedMessage = message if (component || hook || func) { if (component) { formattedMessage = `<${component} />` } else if (hook) { formattedMessage = hook } else if (func) { formattedMessage = `${func}()` } if (callback) { formattedMessage = `${formattedMessage} ${callback}()` } formattedMessage = `${formattedMessage}: ${message}` } super(formattedMessage) this.name = 'MiniError' } } export class MiniNetworkError extends MiniError { constructor(params: Partial<MiniErrorParams>) { super({message: 'Network Error', ...params}) this.name = 'MiniNetworkError' } } export class MiniEntityNotFoundError extends MiniError { constructor(params: Partial<MiniErrorParams>) { super({message: 'Entity Not Found', ...params}) this.name = 'MiniEntityNotFoundError' } } export const formatError = ( source: MiniErrorSource, error: any ): MiniError | MiniNetworkError | null => { if (!error) { return null } if (error.networkError) { return new MiniNetworkError({ ...source, message: error.networkError?.message, }) } if (error.code) { return new MiniError({ ...source, message: `${error.code}${error.message ? `: ${error.message}` : ''}`, }) } return new MiniError({ ...source, message: error?.message, }) }