UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 6.44 kB
//!packages/normalization-core/src/error-coercion.ts const STRUCTURED_ERROR_OWNED_FIELDS=/* @__PURE__ */ new Set([`cause`,`message`,`name`,`stack`]);const STRUCTURED_ERROR_PROTOTYPE_FIELDS=/* @__PURE__ */ new Set([`__proto__`,`constructor`,`prototype`]);const PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND=Symbol.for(`openclaw.provider-auth-persistence-error`);var ProviderAuthPersistenceError=class extends AggregateError{constructor(e,t,{cause:n}){super([t,n],e,{cause:n});this[PROVIDER_AUTH_PERSISTENCE_ERROR_BRAND]=true;this.persistenceError=t;this.cleanupError=n}};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 toStructuredErrorObject(e){if(e instanceof Error)return e;const t=String(e);if((typeof e!==`object`||e===null)&&typeof e!==`function`)return toErrorObject(e,t);const n=new Error(t,{cause:e});try{const t=Reflect.ownKeys(e).filter(t=>(typeof t!==`string`||!STRUCTURED_ERROR_OWNED_FIELDS.has(t)&&!STRUCTURED_ERROR_PROTOTYPE_FIELDS.has(t))&&Reflect.getOwnPropertyDescriptor(e,t)?.enumerable);for(const r of t)try{Object.defineProperty(n,r,{value:Reflect.get(e,r),writable:true,enumerable:true,configurable:true})}catch{}}catch{}return n}function coerceErrorMessage(e){return e instanceof Error?e.message:String(e)}function stringifyNonErrorCause(e){if(e===null)return`null`;if(typeof e===`string`)return e;if(typeof e===`number`||typeof e===`boolean`||typeof e===`bigint`)return String(e);try{return JSON.stringify(e)??Object.prototype.toString.call(e)}catch{return Object.prototype.toString.call(e)}}function mapErrorCauseChain(e,t){const n=[t(e)];let r=e.cause;let i=0;while(r!==void 0&&r!==null&&i<8){if(r instanceof Error){n.push(t(r));r=r.cause}else{n.push(stringifyNonErrorCause(r));r=void 0}i+=1}return n}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 readErrorName(e){if(!e||typeof e!==`object`)return``;const t=e.name;return typeof t===`string`?t:``}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{extractErrorCode as a,formatErrorStackWithMessage as c,readErrorName as d,stringifyNonErrorCause as f,collectNestedErrorCandidates as i,mapErrorCauseChain as l,toStructuredErrorObject as m,coerceErrorMessage as n,extractErrorCodeOrErrno as o,toErrorObject as p,collectErrorGraphCandidates as r,formatErrorMessage as s,ProviderAuthPersistenceError as t,readErrorCauses as u};