UNPKG

@adobe/aio-commerce-lib-core

Version:

Core utilities for AIO Commerce SDK Libraries

15 lines 3.08 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{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:1/0}):super.toString()}};const ISSUE_KIND_TO_ERROR_TITLE={schema:`Schema validation error`,transformation:`Transformation error`,validation:`Input error`};function getReturnChar(isLastItem,withColor){let char=isLastItem?`└── `:`├── `;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){return(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;return`${returnChar} ${`${kindText} ${withColor?whiteBright(`at`):`at`} ${path} ${message}`}`}))?.join(` `)??``}function displayValidationError(error,withColor=!0){let display=issueToDisplay(error.issues,withColor);return`${withColor?whiteBright(error.message):error.message}\n${display}`}var CommerceSdkValidationError=class extends CommerceSdkErrorBase{constructor(message,{issues,...options}){super(message,options),this.issues=issues}display(withColor=!0){return displayValidationError(this,withColor)}};export{CommerceSdkErrorBase,CommerceSdkValidationError};