OpenClaw常见错误处理盘点,OpenClaw常见错误处理方法

常见错误类型

1. API 错误

2. 网络错误

3. 业务错误

错误处理配置

基础配置

# ~/.openclaw/config.yaml
errorHandling:
  retry:
    enabled: true
    maxAttempts: 3
    backoff: "exponential"  # 指数退避
    initialDelay: 1000  # 1 秒
    maxDelay: 30000  # 30 秒

  fallback:
    enabled: true
    model: "deepseek-chat"  # 备用模型
    message: "服务暂时不可用,请稍后重试"

  logging:
    enabled: true
    level: "error"
    file: "~/.openclaw/logs/error.log"

高级配置

errorHandling:
  # 错误分类处理
  handlers:
    - type: "api_error"
      actions:
        - retry
        - fallback
        - notify

    - type: "network_error"
      actions:
        - retry
        - checkNetwork

    - type: "business_error"
      actions:
        - log
        - userMessage

  # 降级策略
  degradation:
    rateLimit:
      threshold: 10  # 10 次/分钟
      action: "queue"  # 排队处理

    errorRate:
      threshold: 0.1  # 10% 错误率
      action: "circuit_breaker"  # 熔断

代码示例

重试逻辑

async function withRetry(fn, maxAttempts = 3) {
  for (let i = 0; i < maxAttempts; i++) {
    try {
      return await fn();
    } catch (error) {
      if (i === maxAttempts - 1) throw error;

      const delay = Math.min(1000 * Math.pow(2, i), 30000);
      await sleep(delay);
    }
  }
}

降级处理

async function chatWithFallback(message) {
  try {
    // 尝试主模型
    return await openclaw.chat(message, { model: "claude-sonnet" });
  } catch (error) {
    console.error("主模型失败,切换备用:", error.message);

    // 降级到 DeepSeek
    return await openclaw.chat(message, { model: "deepseek-chat" });
  }
}

熔断器

class CircuitBreaker {
  constructor(threshold = 5, timeout = 60000) {
    this.failures = 0;
    this.threshold = threshold;
    this.timeout = timeout;
    this.state = "closed";  // closed/open/half-open
    this.lastFailure = null;
  }

  async execute(fn) {
    if (this.state === "open") {
      if (Date.now() - this.lastFailure > this.timeout) {
        this.state = "half-open";
      } else {
        throw new Error("Circuit breaker is open");
      }
    }

    try {
      const result = await fn();
      this.onSuccess();
      return result;
    } catch (error) {
      this.onFailure();
      throw error;
    }
  }

  onSuccess() {
    this.failures = 0;
    this.state = "closed";
  }

  onFailure() {
    this.failures++;
    this.lastFailure = Date.now();

    if (this.failures >= this.threshold) {
      this.state = "open";
    }
  }
}

监控告警

Prometheus 指标

metrics:
  errors_total:
    type: counter
    labels: [type, model, channel]

  error_rate:
    type: gauge
    labels: [model]

  retry_total:
    type: counter
    labels: [model, success]

告警规则

alerts:
  - name: "high_error_rate"
    expr: "error_rate > 0.1"
    duration: 5m
    severity: "warning"
    message: "错误率超过 10%"

  - name: "api_down"
    expr: "errors_total{type='api_error'} > 100"
    duration: 1m
    severity: "critical"
    message: "API 大量错误,请检查"

最佳实践总结

总结 :好的错误处理让系统更稳定,用户体验更好。

4 个赞

错误处理盘点很实用

希望加上错误码对照表

遇到新错误可以来这里查

常见错误基本都覆盖了

@thorn613 错误码对照:401是API Key无效 403是权限不够 429是调用频率超限 500是服务端内部错误 记住这四个就能处理80%的报错

1 个赞

@pipcmd 常见错误基本都覆盖了 但缺了一个很常见的:CORS跨域错误 本地开发时前端调OpenClaw API经常遇到 在配置里加上allowed_origins就行

@opal42 遇到新错误来这里查是个好思路 但建议先看OpenClaw的GitHub Issues 很多错误社区已经有讨论和解决方案了 搜错误关键词就能找到

API报错最常见了

错误处理汇总很实用

收藏了以备不时之需

1 个赞

网络错误怎么排查啊

遇到过好几个里面的错误

1 个赞

排查思路写得很清楚

这个错误我遇到过

1 个赞

收藏备用以防万一

1 个赞

汇总得很全面赞一个

第三个错误我遇到过,解决方案有效

实习生的救命帖

建议加上日志排查方法