import{telemetry as c}from"../../../telemetry/index.js";import{McpErrorCodes as s,McpServerType as d}from"../constants.js";class n extends Error{code;data;requestId;constructor(e,t,o,a){super(t),this.name="McpError",this.code=e,this.data=o,this.requestId=a}createErrorResponse(){const e={jsonrpc:"2.0",error:{code:this.code,message:this.message,...this.data?{data:this.data}:{}},id:this.requestId??null},t=i(this.code);return new Response(JSON.stringify(e),{status:t,headers:{"Content-Type":"application/json"}})}}function i(r){switch(r){case s.InvalidRequest:case s.InvalidParams:return 400;case s.MethodNotFound:return 404;case s.ServerError:return 405;case s.InternalError:default:return 500}}function m(r){return new n(s.ServerError,"Method not allowed",void 0,r).createErrorResponse()}function u(r,e=d.Docs,t){const o=r instanceof Error?r.message:String(r),a=r instanceof Error?r.stack:void 0;return c.sendMcpErrorMessage({server_type:e,message:o,stack:a||""}),new n(s.InternalError,"Internal server error mcp",o,t).createErrorResponse()}function f(r="Invalid request",e){return new n(s.InvalidRequest,r,void 0,e).createErrorResponse()}function h(r="Invalid parameters",e){return new n(s.InvalidParams,r,void 0,e).createErrorResponse()}async function v(r,e,t){try{return await r()}catch(o){return c.sendMcpErrorMessage({server_type:e,message:o?.message||"",stack:o?.stack||""}),t&&t(),u(o,e)}}export{n as McpError,u as createInternalServerError,h as createInvalidParamsError,f as createInvalidRequestError,m as createMethodNotAllowedError,v as withErrorHandling};
