errors.js•4.79 kB
/**
* 自定义错误类
*/
export class MCPError extends Error {
constructor(message, code = 'UNKNOWN_ERROR', details = null) {
super(message);
this.name = 'MCPError';
this.code = code;
this.details = details;
this.timestamp = new Date().toISOString();
}
toJSON() {
return {
name: this.name,
message: this.message,
code: this.code,
details: this.details,
timestamp: this.timestamp,
stack: this.stack,
};
}
}
/**
* GitLab相关错误
*/
export class GitLabError extends MCPError {
constructor(message, details = null) {
super(message, 'GITLAB_ERROR', details);
this.name = 'GitLabError';
}
}
/**
* 企业微信相关错误
*/
export class WeChatError extends MCPError {
constructor(message, details = null) {
super(message, 'WECHAT_ERROR', details);
this.name = 'WeChatError';
}
}
/**
* 配置错误
*/
export class ConfigError extends MCPError {
constructor(message, details = null) {
super(message, 'CONFIG_ERROR', details);
this.name = 'ConfigError';
}
}
/**
* 验证错误
*/
export class ValidationError extends MCPError {
constructor(message, details = null) {
super(message, 'VALIDATION_ERROR', details);
this.name = 'ValidationError';
}
}
/**
* 错误处理工具类
*/
export class ErrorHandler {
/**
* 处理和格式化错误
*/
static handle(error, context = '') {
const timestamp = new Date().toISOString();
if (error instanceof MCPError) {
return {
success: false,
error: {
type: error.name,
code: error.code,
message: error.message,
details: error.details,
context,
timestamp,
},
};
}
// 处理Axios错误
if (error.response) {
return {
success: false,
error: {
type: 'HTTPError',
code: 'HTTP_ERROR',
message: `HTTP ${error.response.status}: ${error.response.statusText}`,
details: {
status: error.response.status,
statusText: error.response.statusText,
data: error.response.data,
url: error.config?.url,
},
context,
timestamp,
},
};
}
// 处理网络错误
if (error.request) {
return {
success: false,
error: {
type: 'NetworkError',
code: 'NETWORK_ERROR',
message: '网络请求失败',
details: {
message: error.message,
code: error.code,
},
context,
timestamp,
},
};
}
// 处理其他错误
return {
success: false,
error: {
type: 'UnknownError',
code: 'UNKNOWN_ERROR',
message: error.message || '未知错误',
details: {
stack: error.stack,
},
context,
timestamp,
},
};
}
/**
* 创建用户友好的错误消息
*/
static createUserMessage(error) {
if (error instanceof GitLabError) {
return `GitLab操作失败: ${error.message}`;
}
if (error instanceof WeChatError) {
return `企业微信操作失败: ${error.message}`;
}
if (error instanceof ConfigError) {
return `配置错误: ${error.message}`;
}
if (error instanceof ValidationError) {
return `参数验证失败: ${error.message}`;
}
return `操作失败: ${error.message || '未知错误'}`;
}
/**
* 验证参数
*/
static validateParams(params, schema) {
const errors = [];
for (const [key, rules] of Object.entries(schema)) {
const value = params[key];
// 检查必需参数
if (rules.required && (value === undefined || value === null || value === '')) {
errors.push(`参数 '${key}' 是必需的`);
continue;
}
// 如果值不存在且不是必需的,跳过其他验证
if (value === undefined || value === null) {
continue;
}
// 检查类型
if (rules.type && typeof value !== rules.type) {
errors.push(`参数 '${key}' 类型错误,期望 ${rules.type},实际 ${typeof value}`);
}
// 检查格式(如日期格式)
if (rules.format === 'date' && !/^\d{4}-\d{2}-\d{2}$/.test(value)) {
errors.push(`参数 '${key}' 格式错误,期望 YYYY-MM-DD 格式`);
}
// 检查枚举值
if (rules.enum && !rules.enum.includes(value)) {
errors.push(`参数 '${key}' 值无效,允许的值: ${rules.enum.join(', ')}`);
}
}
if (errors.length > 0) {
throw new ValidationError('参数验证失败', { errors });
}
}
}