UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 4.73 kB
//!packages/normalization-core/src/error-coercion.ts const PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND=Symbol.for(`openclaw.provider-auth-persistence-error`);function isProviderAuthPersistenceError(e){try{return PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND in e&&e[PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND]===true}catch{return false}}function isErrorObject(e){try{if(e instanceof Error)return true;return Object.prototype.toString.call(e)===`[object Error]`}catch{return false}}function isAggregateErrorObject(e){try{if(e instanceof AggregateError)return true;for(let t=Object.getPrototypeOf(e);t;t=Object.getPrototypeOf(t)){const e=Object.getOwnPropertyDescriptor(t,`constructor`)?.value;if(typeof e===`function`&&e.name===`AggregateError`)return true}}catch{}return false}function readProperty(e,t){try{return e[t]}catch{return}}function readErrorText(e,t){const n=readProperty(e,t);return typeof n===`string`?n:void 0}function formatStatusAndCode(e){if((typeof e!==`object`||e===null)&&typeof e!==`function`)return;try{if(Object.keys(e).some(e=>e!==`status`&&e!==`code`))return}catch{}const t=readProperty(e,`status`);const n=readProperty(e,`code`);if(t===void 0&&n===void 0)return;return`status=${typeof t===`string`||typeof t===`number`?String(t):`unknown`} code=${typeof n===`string`||typeof n===`number`?String(n):`unknown`}`}function stringifyUnknown(e){if(e===null||typeof e!==`object`&&typeof e!==`function`)return String(e);try{const t=JSON.stringify(e);if(t!==void 0)return t}catch{}try{return Object.prototype.toString.call(e)}catch{return`Unknown error`}}function readErrorCauses(e){if(!isErrorObject(e))return[];const t=readProperty(e,`cause`);const n=isAggregateErrorObject(e)?readProperty(e,`errors`):void 0;const r=isProviderAuthPersistenceError(e)?[readProperty(e,`persistenceError`),readProperty(e,`cleanupError`)]:[];const i=readErrorText(e,`name`)===`SuppressedError`?[readProperty(e,`error`),readProperty(e,`suppressed`)].map(e=>e==null?String(e):e):[];return[...r,t||void 0,...Array.isArray(n)?n:[],...i]}function formatErrorMessage(e,t={}){let n;if(isErrorObject(e)){n=readErrorText(e,`message`)||readErrorText(e,`name`)||`Error`;const r=/* @__PURE__ */ new Set([n]);const appendCauseMessage=e=>{if(!e||r.has(e))return;n+=` | ${e}`;r.add(e)};if(t.includeCode){const t=readProperty(e,`code`);if(typeof t===`string`||typeof t===`number`)appendCauseMessage(String(t))}const i=collectErrorGraphCandidates(e,readErrorCauses);for(const e of i.slice(1))if(isErrorObject(e)){const t=readErrorText(e,`message`);if(t&&n.includes(t))r.add(t);else appendCauseMessage(t);const i=readProperty(e,`code`);if(typeof i===`string`||typeof i===`number`)appendCauseMessage(String(i))}else if(typeof e===`string`)appendCauseMessage(e);else appendCauseMessage(formatStatusAndCode(e)??stringifyUnknown(e))}else n=formatStatusAndCode(e)??stringifyUnknown(e);return t.redact?t.redact(n):n}function formatErrorStackWithMessage(e){const t=readErrorText(e,`name`)||`Error`;const n=readErrorText(e,`message`)??``;const r=readProperty(e,`stack`);if(typeof r!==`string`||!r)return n||t;if(!n||r.includes(n))return r;const i=`${t}: ${n}`;const a=r.indexOf(` `);const o=a<0?r:r.slice(0,a);return o===t?i+r.slice(o.length):`${i}\n${r}`}function toErrorObject(e,t){if(e instanceof Error)return e;if(typeof e===`string`)return new Error(e);const n=new Error(t,{cause:e});if(typeof e===`object`&&e!==null||typeof e===`function`)Object.assign(n,e);return n}function coerceErrorMessage(e){return e instanceof Error?e.message:String(e)}function extractErrorCode(e){if(!e||typeof e!==`object`)return;const t=readProperty(e,`code`);return typeof t===`string`||typeof t===`number`?String(t):void 0}function collectErrorGraphCandidates(e,t){if(e==null)return[];const n=[e];const r=(/* @__PURE__ */ new Set()).add(e);for(const e of n){if(!e||typeof e!==`object`||!t)continue;for(const i of t(e))if(i!=null&&!r.has(i)){r.add(i);n.push(i)}}return n}function extractErrorCodeOrErrno(e){const t=extractErrorCode(e);if(t)return t.trim().toUpperCase();if(!e||typeof e!==`object`)return;const n=e.errno;if(typeof n===`string`&&n.trim())return n.trim().toUpperCase();if(typeof n===`number`&&Number.isFinite(n))return String(n)}function collectNestedErrorCandidates(e){return collectErrorGraphCandidates(e,e=>{const t=[readProperty(e,`cause`),readProperty(e,`reason`),readProperty(e,`original`),readProperty(e,`error`),readProperty(e,`suppressed`),readProperty(e,`data`)];const n=readProperty(e,`errors`);if(Array.isArray(n))t.push(...n);return t})}export{extractErrorCodeOrErrno as a,toErrorObject as c,extractErrorCode as i,collectErrorGraphCandidates as n,formatErrorMessage as o,collectNestedErrorCandidates as r,formatErrorStackWithMessage as s,coerceErrorMessage as t};