|
| 1 | +/** |
| 2 | + * Structured error types for agent-consumable error responses. |
| 3 | + */ |
| 4 | + |
| 5 | +export type XintErrorCode = |
| 6 | + | "RATE_LIMITED" |
| 7 | + | "AUTH_FAILED" |
| 8 | + | "NOT_FOUND" |
| 9 | + | "BUDGET_DENIED" |
| 10 | + | "POLICY_DENIED" |
| 11 | + | "VALIDATION_ERROR" |
| 12 | + | "TIMEOUT" |
| 13 | + | "API_ERROR"; |
| 14 | + |
| 15 | +export interface XintError { |
| 16 | + code: XintErrorCode; |
| 17 | + message: string; |
| 18 | + retryable: boolean; |
| 19 | + retry_after_ms?: number; |
| 20 | + suggestion?: string; |
| 21 | + failing_input?: string; |
| 22 | +} |
| 23 | + |
| 24 | +export function rateLimited(waitMs: number): XintError { |
| 25 | + return { |
| 26 | + code: "RATE_LIMITED", |
| 27 | + message: `Rate limited. Retry after ${waitMs}ms.`, |
| 28 | + retryable: true, |
| 29 | + retry_after_ms: waitMs, |
| 30 | + suggestion: `Wait ${waitMs}ms before retrying.`, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +export function authFailed(detail?: string): XintError { |
| 35 | + return { |
| 36 | + code: "AUTH_FAILED", |
| 37 | + message: detail || "Authentication failed.", |
| 38 | + retryable: false, |
| 39 | + suggestion: "Set X_BEARER_TOKEN env var or run 'xint auth setup'.", |
| 40 | + }; |
| 41 | +} |
| 42 | + |
| 43 | +export function notFound(resource?: string): XintError { |
| 44 | + return { |
| 45 | + code: "NOT_FOUND", |
| 46 | + message: resource ? `Not found: ${resource}` : "Resource not found.", |
| 47 | + retryable: false, |
| 48 | + suggestion: "The tweet or user may have been deleted.", |
| 49 | + failing_input: resource, |
| 50 | + }; |
| 51 | +} |
| 52 | + |
| 53 | +export function budgetDenied(spent: number, limit: number, remaining: number): XintError { |
| 54 | + return { |
| 55 | + code: "BUDGET_DENIED", |
| 56 | + message: `Daily budget exceeded ($${spent.toFixed(2)} / $${limit.toFixed(2)}).`, |
| 57 | + retryable: false, |
| 58 | + suggestion: "Use 'xint costs budget set N' to increase the daily limit.", |
| 59 | + }; |
| 60 | +} |
| 61 | + |
| 62 | +export function policyDenied(tool: string, current: string, required: string): XintError { |
| 63 | + return { |
| 64 | + code: "POLICY_DENIED", |
| 65 | + message: `Tool '${tool}' requires '${required}' policy mode (current: '${current}').`, |
| 66 | + retryable: false, |
| 67 | + suggestion: `Start MCP with --policy=${required}.`, |
| 68 | + failing_input: tool, |
| 69 | + }; |
| 70 | +} |
| 71 | + |
| 72 | +export function validationError(param: string, reason: string): XintError { |
| 73 | + return { |
| 74 | + code: "VALIDATION_ERROR", |
| 75 | + message: `Parameter '${param}': ${reason}`, |
| 76 | + retryable: false, |
| 77 | + suggestion: `Fix parameter '${param}': ${reason}`, |
| 78 | + failing_input: param, |
| 79 | + }; |
| 80 | +} |
| 81 | + |
| 82 | +export function timeout(detail?: string): XintError { |
| 83 | + return { |
| 84 | + code: "TIMEOUT", |
| 85 | + message: detail || "Request timed out.", |
| 86 | + retryable: true, |
| 87 | + retry_after_ms: 5000, |
| 88 | + suggestion: "Retry in 5s.", |
| 89 | + }; |
| 90 | +} |
| 91 | + |
| 92 | +export function apiError(status: number, detail?: string): XintError { |
| 93 | + return { |
| 94 | + code: "API_ERROR", |
| 95 | + message: detail || `X API error (HTTP ${status}).`, |
| 96 | + retryable: status >= 500, |
| 97 | + retry_after_ms: status >= 500 ? 30000 : undefined, |
| 98 | + suggestion: status >= 500 ? "X API issues. Retry in 30s." : undefined, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Classify an error from API calls into a structured XintError. |
| 104 | + * Parses error message patterns to detect HTTP status codes. |
| 105 | + */ |
| 106 | +export function classifyError(err: unknown): XintError { |
| 107 | + const msg = err instanceof Error ? err.message : String(err); |
| 108 | + |
| 109 | + // Rate limited (429) |
| 110 | + const rateMatch = msg.match(/Rate limited.*?(\d+)s/i); |
| 111 | + if (rateMatch || msg.includes("429")) { |
| 112 | + const waitSec = rateMatch ? parseInt(rateMatch[1]) : 60; |
| 113 | + return rateLimited(waitSec * 1000); |
| 114 | + } |
| 115 | + |
| 116 | + // Auth failed (401) |
| 117 | + if (msg.includes("401") || msg.includes("OAuth token rejected") || msg.includes("X_BEARER_TOKEN not found")) { |
| 118 | + return authFailed(msg); |
| 119 | + } |
| 120 | + |
| 121 | + // Not found (404) |
| 122 | + if (msg.includes("404") || msg.includes("not found")) { |
| 123 | + return notFound(); |
| 124 | + } |
| 125 | + |
| 126 | + // Budget denied |
| 127 | + if (msg.includes("BUDGET_DENIED") || msg.includes("budget exceeded")) { |
| 128 | + try { |
| 129 | + const parsed = JSON.parse(msg); |
| 130 | + return budgetDenied(parsed.spent_usd || 0, parsed.limit_usd || 0, parsed.remaining_usd || 0); |
| 131 | + } catch { |
| 132 | + return budgetDenied(0, 0, 0); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + // Policy denied |
| 137 | + if (msg.includes("POLICY_DENIED")) { |
| 138 | + try { |
| 139 | + const parsed = JSON.parse(msg); |
| 140 | + return policyDenied(parsed.message || "", parsed.policy_mode || "", parsed.required_mode || ""); |
| 141 | + } catch { |
| 142 | + return policyDenied("unknown", "unknown", "unknown"); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + // Timeout |
| 147 | + if (msg.toLowerCase().includes("timeout") || msg.toLowerCase().includes("timed out")) { |
| 148 | + return timeout(msg); |
| 149 | + } |
| 150 | + |
| 151 | + // Generic API error — try to extract status |
| 152 | + const statusMatch = msg.match(/X API (\d{3})/); |
| 153 | + if (statusMatch) { |
| 154 | + return apiError(parseInt(statusMatch[1]), msg); |
| 155 | + } |
| 156 | + |
| 157 | + // Fallback |
| 158 | + return apiError(0, msg); |
| 159 | +} |
0 commit comments