import{InstrumentationBase as i}from"@opentelemetry/instrumentation";class o extends i{sendErrorEvent;originalConsoleError;constructor(r,e){super("errors","1.0",r||{}),this.sendErrorEvent=e}static toErrorEvent(r,e){return{type:"error",payload:{handler:e,error_type:o.getErrorType(r),message:o.getErrorMessage(r),stack:o.getErrorStack(r)}}}enable(){window.addEventListener("error",this.onError),window.addEventListener("unhandledrejection",this.onUnhandledRejection),this.wrapConsoleError()}disable(){window.removeEventListener("error",this.onError),window.removeEventListener("unhandledrejection",this.onUnhandledRejection),this.unwrapConsoleError()}init(){}static getErrorMessage(r){return r?(typeof r=="object"&&"message"in r?String(r.message):String(r)).trim():void 0}static getErrorStack(r){if(!r||typeof r!="object"||!("stack"in r)||!r.stack)return;const e=String(r.stack);return e.length>4096?e.substring(0,4096):e}static getErrorType(r){return!r||typeof r!="object"?typeof r:"name"in r?String(r.name):"constructor"in r&&"name"in r.constructor?String(r.constructor.name):"Error"}static combineConsoleErrorArgs(r){const e=r.findIndex(n=>n instanceof Error),t=r.filter((n,s)=>s!==e).map(n=>String(n)).join(" ");if(e===-1)return t;{const n=r[e];return t&&(n.message=`${n.message} ${t}`),n}}onError=r=>{this.sendError(r.error,"window.onerror")};onUnhandledRejection=r=>{this.sendError(r.reason,"window.onunhandledrejection")};wrapConsoleError(){this.originalConsoleError=console.error,console.error=(...r)=>{try{const e=o.combineConsoleErrorArgs(r);this.sendError(e,"console.error")}catch(e){process.env.NODE_ENV!=="production"&&this.originalConsoleError?.call(console,"Error reporting failed:",e)}this.originalConsoleError?.apply(console,r)}}unwrapConsoleError(){this.originalConsoleError&&(console.error=this.originalConsoleError,this.originalConsoleError=void 0)}sendError(r,e){this.sendErrorEvent(o.toErrorEvent(r,e))}}export{o as ErrorInstrumentation};
