UNPKG

@adobe/aio-commerce-lib-core

Version:

Core utilities for AIO Commerce SDK Libraries

91 lines (90 loc) 3.61 kB
/** * @license * * Copyright 2025 Adobe. All rights reserved. * This file is licensed to you under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. You may obtain a copy * of the License at http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software distributed under * the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS * OF ANY KIND, either express or implied. See the License for the specific language * governing permissions and limitations under the License. */ import util from "util"; import { cyanBright, dim, whiteBright, yellowBright } from "ansis"; import { getDotPath } from "valibot"; var CommerceSdkErrorBase = class CommerceSdkErrorBase extends Error { traceId; constructor(message, options) { let { traceId,...baseOptions } = options ?? {}; super(message, baseOptions), Object.setPrototypeOf(this, new.target.prototype), Error.captureStackTrace && Error.captureStackTrace(this, new.target), this.name = new.target.name || "CommerceSdkError", this.traceId = traceId; } static isSdkError(error) { return error instanceof CommerceSdkErrorBase; } get fullStack() { let out = this.stack ?? "", cause = this.cause; for (; cause instanceof Error;) out += `\nCaused by: ${cause.stack ?? cause.message}`, cause = cause.cause; return out; } get rootCause() { let cause = this.cause; for (; cause && typeof cause == "object" && cause && "cause" in cause;) cause = cause.cause; return cause; } toJSON() { return { name: this.name, message: this.message, stack: this.fullStack, cause: this.cause, traceId: this.traceId }; } toString(inspect = !0) { return inspect ? util.inspect(this, { depth: null, colors: !0, sorted: !0, maxStringLength: Infinity }) : super.toString(); } }; const LAST_RETURN_CHAR = "└── ", RETURN_CHAR = "├── ", ISSUE_KIND_TO_ERROR_TITLE = { schema: "Schema validation error", transformation: "Transformation error", validation: "Input error" }; function getReturnChar(isLastItem, withColor) { let char = isLastItem ? LAST_RETURN_CHAR : RETURN_CHAR; return withColor ? cyanBright(char) : char; } function getKindText(kind, withColor) { let text = ISSUE_KIND_TO_ERROR_TITLE[kind] ?? "Unknown issue kind"; return withColor ? yellowBright(text) : text; } function getPathText(dotPath, withColor) { return dotPath ? withColor ? `${cyanBright(dotPath)}${whiteBright(dim(" →"))}` : `${dotPath} →` : ""; } function issueToDisplay(issues, withColor = !0) { let lines = issues?.map((issue, index) => { let returnChar = getReturnChar(index === issues.length - 1, withColor), kindText = getKindText(issue.kind, withColor), path = getPathText(getDotPath(issue), withColor), message = withColor ? whiteBright(issue.message) : issue.message, at = withColor ? whiteBright("at") : "at", issueLine = `${kindText} ${at} ${path} ${message}`; return `${returnChar} ${issueLine}`; }); return lines?.join("\n") ?? ""; } function displayValidationError(error, withColor = !0) { let display = issueToDisplay(error.issues, withColor), message = withColor ? whiteBright(error.message) : error.message; return `${message}\n${display}`; } var CommerceSdkValidationError = class extends CommerceSdkErrorBase { issues; constructor(message, { issues,...options }) { super(message, options), this.issues = issues; } display(withColor = !0) { return displayValidationError(this, withColor); } }; export { CommerceSdkErrorBase, CommerceSdkValidationError };