File size: 6,424 Bytes
9284ad7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
/**

 * Centralized Error Handling

 * Standardizes error handling across the codebase

 */

import pino from 'pino'

const logger = pino({ level: process.env.LOG_LEVEL || 'info' })

/**

 * Error severity levels

 */
export type ErrorSeverity = 'critical' | 'high' | 'medium' | 'low' | 'info'

/**

 * Standardized error structure

 */
export interface PRIXError {
  message: string
  severity: ErrorSeverity
  code: string
  context?: Record<string, any>
  cause?: Error
}

/**

 * Error codes for different failure modes

 */
export const ErrorCodes = {
  // AI/LLM errors
  AI_RATE_LIMIT: 'AI_RATE_LIMIT',
  AI_CONTEXT_TOO_LARGE: 'AI_CONTEXT_TOO_LARGE',
  AI_TIMEOUT: 'AI_TIMEOUT',
  AI_AUTHENTICATION: 'AI_AUTHENTICATION',
  AI_SERVICE_UNAVAILABLE: 'AI_SERVICE_UNAVAILABLE',

  // GitHub API errors
  GITHUB_RATE_LIMIT: 'GITHUB_RATE_LIMIT',
  GITHUB_AUTHENTICATION: 'GITHUB_AUTHENTICATION',
  GITHUB_NOT_FOUND: 'GITHUB_NOT_FOUND',
  GITHUB_CONFLICT: 'GITHUB_CONFLICT',
  GITHUB_TIMEOUT: 'GITHUB_TIMEOUT',

  // File system errors
  FS_READ_ERROR: 'FS_READ_ERROR',
  FS_WRITE_ERROR: 'FS_WRITE_ERROR',
  FS_NOT_FOUND: 'FS_NOT_FOUND',

  // Parse/analysis errors
  PARSE_ERROR: 'PARSE_ERROR',
  AST_ANALYSIS_ERROR: 'AST_ANALYSIS_ERROR',
  PATCH_PARSE_ERROR: 'PATCH_PARSE_ERROR',

  // Context errors
  CONTEXT_MISSING: 'CONTEXT_MISSING',
  INVALID_PAYLOAD: 'INVALID_PAYLOAD',

  // Circuit breaker
  CIRCUIT_OPEN: 'CIRCUIT_OPEN',

  // Unknown
  UNKNOWN: 'UNKNOWN'
} as const

/**

 * Create a standardized error

 */
export const createError = (
  message: string,
  severity: ErrorSeverity,
  code: string,
  context?: Record<string, any>,
  cause?: Error
): PRIXError => ({
  message,
  severity,
  code,
  context,
  cause
})

/**

 * Log error with appropriate level based on severity

 */
export const logError = (error: PRIXError): void => {
  const logData = {
    code: error.code,
    severity: error.severity,
    context: error.context,
    cause: error.cause?.message
  }

  switch (error.severity) {
    case 'critical':
      logger.fatal(logData, error.message)
      break
    case 'high':
      logger.error(logData, error.message)
      break
    case 'medium':
      logger.warn(logData, error.message)
      break
    case 'low':
      logger.info(logData, error.message)
      break
    case 'info':
      logger.debug(logData, error.message)
      break
  }
}

/**

 * Safe wrapper for async functions

 * Standardizes error handling

 */
export const safeExecute = async <T>(
  fn: () => Promise<T>,
  errorCode: string,
  severity: ErrorSeverity = 'medium',
  context?: Record<string, any>,
  fallback?: T
): Promise<T | undefined> => {
  try {
    return await fn()
  } catch (error) {
    const prixError = createError(
      error instanceof Error ? error.message : String(error),
      severity,
      errorCode,
      context,
      error instanceof Error ? error : undefined
    )
    logError(prixError)
    return fallback
  }
}

/**

 * Safe wrapper for sync functions

 */
export const safeExecuteSync = <T>(
  fn: () => T,
  errorCode: string,
  severity: ErrorSeverity = 'medium',
  context?: Record<string, any>,
  fallback?: T
): T | undefined => {
  try {
    return fn()
  } catch (error) {
    const prixError = createError(
      error instanceof Error ? error.message : String(error),
      severity,
      errorCode,
      context,
      error instanceof Error ? error : undefined
    )
    logError(prixError)
    return fallback
  }
}

/**

 * Classify error from external service

 */
export const classifyError = (error: any): PRIXError => {
  // AI API errors
  if (error?.status === 429) {
    return createError('AI rate limit exceeded', 'high', ErrorCodes.AI_RATE_LIMIT, { status: 429 })
  }
  if (error?.status === 413) {
    return createError('AI context too large', 'high', ErrorCodes.AI_CONTEXT_TOO_LARGE, { status: 413 })
  }
  if (error?.code === 'ETIMEDOUT' || error?.code === 'ECONNABORTED') {
    return createError('AI timeout', 'medium', ErrorCodes.AI_TIMEOUT, { code: error.code })
  }
  if (error?.status === 401) {
    return createError('AI authentication failed', 'critical', ErrorCodes.AI_AUTHENTICATION, { status: 401 })
  }
  if (error?.status === 503 || error?.status === 502) {
    return createError('AI service unavailable', 'high', ErrorCodes.AI_SERVICE_UNAVAILABLE, { status: error.status })
  }

  // GitHub API errors
  if (error?.status === 403 && error?.message?.includes('rate limit')) {
    return createError('GitHub rate limit exceeded', 'high', ErrorCodes.GITHUB_RATE_LIMIT, { status: 403 })
  }
  if (error?.status === 401) {
    return createError('GitHub authentication failed', 'critical', ErrorCodes.GITHUB_AUTHENTICATION, { status: 401 })
  }
  if (error?.status === 404) {
    return createError('GitHub resource not found', 'medium', ErrorCodes.GITHUB_NOT_FOUND, { status: 404 })
  }
  if (error?.status === 409) {
    return createError('GitHub conflict', 'medium', ErrorCodes.GITHUB_CONFLICT, { status: 409 })
  }

  // Default
  return createError(
    error?.message || 'Unknown error',
    'medium',
    ErrorCodes.UNKNOWN,
    { originalError: error }
  )
}

/**

 * Retry configuration

 */
export interface RetryConfig {
  maxRetries: number
  baseDelay: number
  maxDelay: number
  retryableCodes: string[]
}

/**

 * Default retry configuration

 */
export const defaultRetryConfig: RetryConfig = {
  maxRetries: 3,
  baseDelay: 1000,
  maxDelay: 30000,
  retryableCodes: [
    ErrorCodes.AI_RATE_LIMIT,
    ErrorCodes.AI_TIMEOUT,
    ErrorCodes.AI_SERVICE_UNAVAILABLE,
    ErrorCodes.GITHUB_RATE_LIMIT,
    ErrorCodes.GITHUB_TIMEOUT
  ]
}

/**

 * Check if error should be retried

 */
export const shouldRetry = (error: PRIXError, config: RetryConfig = defaultRetryConfig): boolean => {
  return config.retryableCodes.includes(error.code)
}

/**

 * Calculate delay for retry with exponential backoff

 */
export const calculateRetryDelay = (attempt: number, config: RetryConfig = defaultRetryConfig): number => {
  const delay = Math.min(
    config.baseDelay * Math.pow(2, attempt),
    config.maxDelay
  )
  // Add jitter to prevent thundering herd
  return delay + Math.random() * 1000
}