@techsio/storefront-data
Version:
Medusa-first storefront data layer built on TanStack Query.
28 lines (27 loc) • 1.19 kB
JavaScript
const hasStringMessage = (error)=>Boolean(error && "object" == typeof error && "message" in error && "string" == typeof error.message);
const hasStringCode = (error)=>Boolean(error && "object" == typeof error && "code" in error && "string" == typeof error.code);
const toErrorMessage = (error)=>{
if (!error) return null;
if (error instanceof Error) return error.message;
if (hasStringMessage(error)) return error.message;
if ("string" == typeof error) return error;
const serialized = String(error);
return "[object Object]" === serialized ? null : serialized;
};
const toErrorMessageWithFallback = (error, fallback)=>toErrorMessage(error) ?? fallback;
const toErrorWithCode = (error, fallback)=>{
const message = toErrorMessage(error);
if (hasStringCode(error)) return {
message: message ?? fallback,
code: error.code
};
return {
message: message ?? fallback
};
};
const createErrorWithStage = (stage, fallback, cause)=>({
stage,
message: toErrorMessageWithFallback(cause, fallback),
cause
});
export { createErrorWithStage, toErrorMessage, toErrorMessageWithFallback, toErrorWithCode };