@copilotkit/runtime
Version:
<img src="https://github.com/user-attachments/assets/0a6b64d9-e193-4940-a3f6-60334ac34084" alt="banner" style="border-radius: 12px; border: 2px solid #d6d4fa;" />
1 lines • 3.84 kB
Source Map (JSON)
{"version":3,"sources":["../src/service-adapters/shared/error-utils.ts"],"sourcesContent":["import { CopilotKitLowLevelError, CopilotKitErrorCode } from \"@copilotkit/shared\";\n\n/**\n * Converts service adapter errors to structured CopilotKitError format using HTTP status codes.\n * This provides consistent error classification across all service adapters.\n */\nexport function convertServiceAdapterError(\n error: any,\n adapterName: string,\n): CopilotKitLowLevelError {\n const errorName = error?.constructor?.name || error.name;\n const errorMessage = error?.message || String(error);\n const statusCode = error.status || error.statusCode || error.response?.status;\n const responseData = error.error || error.response?.data || error.data;\n\n // Create the base error with the constructor signature\n const structuredError = new CopilotKitLowLevelError({\n error: error instanceof Error ? error : new Error(errorMessage),\n url: `${adapterName} service adapter`,\n message: `${adapterName} API error: ${errorMessage}`,\n });\n\n // Add additional properties after construction\n if (statusCode) {\n (structuredError as any).statusCode = statusCode;\n }\n if (responseData) {\n (structuredError as any).responseData = responseData;\n }\n if (errorName) {\n (structuredError as any).originalErrorType = errorName;\n }\n\n // Classify error based on HTTP status codes (reliable and provider-agnostic)\n let newCode: CopilotKitErrorCode;\n\n if (statusCode === 401) {\n // 401 = Authentication/API key issues\n newCode = CopilotKitErrorCode.AUTHENTICATION_ERROR;\n } else if (statusCode >= 400 && statusCode < 500) {\n // 4xx = Client errors (bad request, invalid params, etc.) - these are configuration issues\n newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;\n } else if (statusCode >= 500) {\n // 5xx = Server errors - keep as NETWORK_ERROR since it's infrastructure related\n newCode = CopilotKitErrorCode.NETWORK_ERROR;\n } else if (statusCode) {\n // Any other HTTP status with an error - likely configuration\n newCode = CopilotKitErrorCode.CONFIGURATION_ERROR;\n } else {\n // No status code - likely a genuine network/connection error\n newCode = CopilotKitErrorCode.NETWORK_ERROR;\n }\n\n // Update both the instance property and the extensions\n (structuredError as any).code = newCode;\n if ((structuredError as any).extensions) {\n (structuredError as any).extensions.code = newCode;\n }\n\n return structuredError;\n}\n"],"mappings":";;;;;AAAA,SAASA,yBAAyBC,2BAA2B;AAMtD,SAASC,2BACdC,OACAC,aAAmB;AARrB;AAUE,QAAMC,cAAYF,oCAAOG,gBAAPH,mBAAoBI,SAAQJ,MAAMI;AACpD,QAAMC,gBAAeL,+BAAOM,YAAWC,OAAOP,KAAAA;AAC9C,QAAMQ,aAAaR,MAAMS,UAAUT,MAAMQ,gBAAcR,WAAMU,aAANV,mBAAgBS;AACvE,QAAME,eAAeX,MAAMA,WAASA,WAAMU,aAANV,mBAAgBY,SAAQZ,MAAMY;AAGlE,QAAMC,kBAAkB,IAAIC,wBAAwB;IAClDd,OAAOA,iBAAiBe,QAAQf,QAAQ,IAAIe,MAAMV,YAAAA;IAClDW,KAAK,GAAGf;IACRK,SAAS,GAAGL,0BAA0BI;EACxC,CAAA;AAGA,MAAIG,YAAY;AACbK,oBAAwBL,aAAaA;EACxC;AACA,MAAIG,cAAc;AACfE,oBAAwBF,eAAeA;EAC1C;AACA,MAAIT,WAAW;AACZW,oBAAwBI,oBAAoBf;EAC/C;AAGA,MAAIgB;AAEJ,MAAIV,eAAe,KAAK;AAEtBU,cAAUC,oBAAoBC;EAChC,WAAWZ,cAAc,OAAOA,aAAa,KAAK;AAEhDU,cAAUC,oBAAoBE;EAChC,WAAWb,cAAc,KAAK;AAE5BU,cAAUC,oBAAoBG;EAChC,WAAWd,YAAY;AAErBU,cAAUC,oBAAoBE;EAChC,OAAO;AAELH,cAAUC,oBAAoBG;EAChC;AAGCT,kBAAwBU,OAAOL;AAChC,MAAKL,gBAAwBW,YAAY;AACtCX,oBAAwBW,WAAWD,OAAOL;EAC7C;AAEA,SAAOL;AACT;AAtDgBd;","names":["CopilotKitLowLevelError","CopilotKitErrorCode","convertServiceAdapterError","error","adapterName","errorName","constructor","name","errorMessage","message","String","statusCode","status","response","responseData","data","structuredError","CopilotKitLowLevelError","Error","url","originalErrorType","newCode","CopilotKitErrorCode","AUTHENTICATION_ERROR","CONFIGURATION_ERROR","NETWORK_ERROR","code","extensions"]}