-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy patherrors.go
More file actions
400 lines (351 loc) · 12 KB
/
errors.go
File metadata and controls
400 lines (351 loc) · 12 KB
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
package gollama
import (
"errors"
"fmt"
)
// Error types for different categories of errors
var (
// Library errors
ErrLibraryNotLoaded = errors.New("llama.cpp library not loaded")
ErrLibraryLoadFailed = errors.New("failed to load llama.cpp library")
ErrFunctionNotFound = errors.New("function not found in library")
ErrInvalidLibraryPath = errors.New("invalid library path")
// Model errors
ErrModelNotLoaded = errors.New("model not loaded")
ErrModelLoadFailed = errors.New("failed to load model")
ErrModelSaveFailed = errors.New("failed to save model")
ErrInvalidModelPath = errors.New("invalid model path")
ErrModelCorrupted = errors.New("model file corrupted")
ErrUnsupportedModelType = errors.New("unsupported model type")
// Context errors
ErrContextNotCreated = errors.New("context not created")
ErrContextCreationFailed = errors.New("failed to create context")
ErrInvalidContextSize = errors.New("invalid context size")
ErrContextFull = errors.New("context is full")
// Token errors
ErrTokenizationFailed = errors.New("tokenization failed")
ErrInvalidToken = errors.New("invalid token")
ErrTokenOutOfRange = errors.New("token out of vocabulary range")
// Generation errors
ErrGenerationFailed = errors.New("text generation failed")
ErrSamplingFailed = errors.New("token sampling failed")
ErrInvalidSamplingParams = errors.New("invalid sampling parameters")
// Memory errors
ErrOutOfMemory = errors.New("out of memory")
ErrMemoryAllocationFailed = errors.New("memory allocation failed")
ErrInvalidMemorySize = errors.New("invalid memory size")
// Configuration errors
ErrInvalidConfig = errors.New("invalid configuration")
ErrConfigValidationFailed = errors.New("configuration validation failed")
ErrUnsupportedPlatform = errors.New("unsupported platform")
// Backend errors
ErrBackendNotAvailable = errors.New("backend not available")
ErrBackendInitFailed = errors.New("backend initialization failed")
ErrGPUNotAvailable = errors.New("GPU not available")
ErrCUDANotAvailable = errors.New("CUDA not available")
ErrMetalNotAvailable = errors.New("metal backend not available")
ErrVulkanNotAvailable = errors.New("vulkan backend not available")
// File I/O errors
ErrFileNotFound = errors.New("file not found")
ErrFileReadFailed = errors.New("failed to read file")
ErrFileWriteFailed = errors.New("failed to write file")
ErrInvalidFileFormat = errors.New("invalid file format")
// Parameter errors
ErrInvalidParameter = errors.New("invalid parameter")
ErrParameterOutOfRange = errors.New("parameter out of range")
ErrMissingParameter = errors.New("missing required parameter")
// Thread/concurrency errors
ErrThreadingFailed = errors.New("threading operation failed")
ErrConcurrencyViolation = errors.New("concurrency violation")
ErrDeadlock = errors.New("deadlock detected")
)
// LlamaError represents a structured error from the llama.cpp library
type LlamaError struct {
Code int `json:"code"`
Message string `json:"message"`
Function string `json:"function,omitempty"`
File string `json:"file,omitempty"`
Line int `json:"line,omitempty"`
Cause error `json:"cause,omitempty"`
}
// Error implements the error interface
func (e *LlamaError) Error() string {
if e.Function != "" {
return fmt.Sprintf("llama error [%d]: %s (in %s)", e.Code, e.Message, e.Function)
}
return fmt.Sprintf("llama error [%d]: %s", e.Code, e.Message)
}
// Unwrap returns the underlying cause
func (e *LlamaError) Unwrap() error {
return e.Cause
}
// Is implements error matching
func (e *LlamaError) Is(target error) bool {
if t, ok := target.(*LlamaError); ok {
return e.Code == t.Code
}
return errors.Is(e.Cause, target)
}
// NewLlamaError creates a new LlamaError
func NewLlamaError(code int, message string) *LlamaError {
return &LlamaError{
Code: code,
Message: message,
}
}
// NewLlamaErrorWithCause creates a new LlamaError with an underlying cause
func NewLlamaErrorWithCause(code int, message string, cause error) *LlamaError {
return &LlamaError{
Code: code,
Message: message,
Cause: cause,
}
}
// NewLlamaErrorWithContext creates a new LlamaError with function context
func NewLlamaErrorWithContext(code int, message, function string) *LlamaError {
return &LlamaError{
Code: code,
Message: message,
Function: function,
}
}
// Error codes matching llama.cpp return values
const (
LLAMA_ERR_SUCCESS = 0
LLAMA_ERR_FAIL = -1
LLAMA_ERR_INVALID_PARAM = -2
LLAMA_ERR_OUT_OF_MEMORY = -3
LLAMA_ERR_FILE_NOT_FOUND = -4
LLAMA_ERR_FILE_READ = -5
LLAMA_ERR_FILE_WRITE = -6
LLAMA_ERR_INVALID_FORMAT = -7
LLAMA_ERR_UNSUPPORTED = -8
LLAMA_ERR_BACKEND_INIT = -9
LLAMA_ERR_CONTEXT_FULL = -10
LLAMA_ERR_TOKEN_INVALID = -11
LLAMA_ERR_MODEL_CORRUPTED = -12
LLAMA_ERR_GPU_UNAVAILABLE = -13
)
// ErrorfromCode converts a llama.cpp error code to a Go error
func ErrorfromCode(code int) error {
switch code {
case LLAMA_ERR_SUCCESS:
return nil
case LLAMA_ERR_FAIL:
return NewLlamaError(code, "operation failed")
case LLAMA_ERR_INVALID_PARAM:
return NewLlamaErrorWithCause(code, "invalid parameter", ErrInvalidParameter)
case LLAMA_ERR_OUT_OF_MEMORY:
return NewLlamaErrorWithCause(code, "out of memory", ErrOutOfMemory)
case LLAMA_ERR_FILE_NOT_FOUND:
return NewLlamaErrorWithCause(code, "file not found", ErrFileNotFound)
case LLAMA_ERR_FILE_READ:
return NewLlamaErrorWithCause(code, "file read error", ErrFileReadFailed)
case LLAMA_ERR_FILE_WRITE:
return NewLlamaErrorWithCause(code, "file write error", ErrFileWriteFailed)
case LLAMA_ERR_INVALID_FORMAT:
return NewLlamaErrorWithCause(code, "invalid file format", ErrInvalidFileFormat)
case LLAMA_ERR_UNSUPPORTED:
return NewLlamaErrorWithCause(code, "unsupported operation", ErrUnsupportedPlatform)
case LLAMA_ERR_BACKEND_INIT:
return NewLlamaErrorWithCause(code, "backend initialization failed", ErrBackendInitFailed)
case LLAMA_ERR_CONTEXT_FULL:
return NewLlamaErrorWithCause(code, "context is full", ErrContextFull)
case LLAMA_ERR_TOKEN_INVALID:
return NewLlamaErrorWithCause(code, "invalid token", ErrInvalidToken)
case LLAMA_ERR_MODEL_CORRUPTED:
return NewLlamaErrorWithCause(code, "model file corrupted", ErrModelCorrupted)
case LLAMA_ERR_GPU_UNAVAILABLE:
return NewLlamaErrorWithCause(code, "GPU not available", ErrGPUNotAvailable)
default:
return NewLlamaError(code, fmt.Sprintf("unknown error code: %d", code))
}
}
// ErrorHandler provides centralized error handling and logging
type ErrorHandler struct {
enableLogging bool
logCallback func(level int, message string)
}
// NewErrorHandler creates a new error handler
func NewErrorHandler(enableLogging bool) *ErrorHandler {
return &ErrorHandler{
enableLogging: enableLogging,
}
}
// SetLogCallback sets the log callback function
func (eh *ErrorHandler) SetLogCallback(callback func(level int, message string)) {
eh.logCallback = callback
}
// HandleError processes and logs an error
func (eh *ErrorHandler) HandleError(err error, context string) error {
if err == nil {
return nil
}
if eh.enableLogging && eh.logCallback != nil {
message := fmt.Sprintf("Error in %s: %v", context, err)
eh.logCallback(3, message) // LLAMA_LOG_LEVEL_ERROR
}
// Wrap with context if it's not already a LlamaError
if _, ok := err.(*LlamaError); !ok {
return NewLlamaErrorWithContext(-1, err.Error(), context)
}
return err
}
// WrapError wraps an error with additional context
func WrapError(err error, message string) error {
if err == nil {
return nil
}
return fmt.Errorf("%s: %w", message, err)
}
// WrapErrorf wraps an error with formatted additional context
func WrapErrorf(err error, format string, args ...interface{}) error {
if err == nil {
return nil
}
return fmt.Errorf(format+": %w", append(args, err)...)
}
// IsRetryableError checks if an error is retryable
func IsRetryableError(err error) bool {
if err == nil {
return false
}
// Check for specific retryable errors
return errors.Is(err, ErrOutOfMemory) ||
errors.Is(err, ErrFileReadFailed) ||
errors.Is(err, ErrBackendInitFailed)
}
// IsFatalError checks if an error is fatal and should stop execution
func IsFatalError(err error) bool {
if err == nil {
return false
}
// Check for specific fatal errors
return errors.Is(err, ErrModelCorrupted) ||
errors.Is(err, ErrInvalidFileFormat) ||
errors.Is(err, ErrUnsupportedPlatform) ||
errors.Is(err, ErrLibraryNotLoaded)
}
// ErrorCategory represents different categories of errors
type ErrorCategory int
const (
CategoryLibrary ErrorCategory = iota
CategoryModel
CategoryContext
CategoryToken
CategoryGeneration
CategoryMemory
CategoryConfig
CategoryBackend
CategoryFile
CategoryParameter
CategoryThread
)
// String returns the string representation of an error category
func (ec ErrorCategory) String() string {
switch ec {
case CategoryLibrary:
return "Library"
case CategoryModel:
return "Model"
case CategoryContext:
return "Context"
case CategoryToken:
return "Token"
case CategoryGeneration:
return "Generation"
case CategoryMemory:
return "Memory"
case CategoryConfig:
return "Config"
case CategoryBackend:
return "Backend"
case CategoryFile:
return "File"
case CategoryParameter:
return "Parameter"
case CategoryThread:
return "Thread"
default:
return "Unknown"
}
}
// CategorizeError determines the category of an error
func CategorizeError(err error) ErrorCategory {
if err == nil {
return CategoryLibrary // Default category
}
switch {
case errors.Is(err, ErrLibraryNotLoaded), errors.Is(err, ErrLibraryLoadFailed):
return CategoryLibrary
case errors.Is(err, ErrModelNotLoaded), errors.Is(err, ErrModelLoadFailed):
return CategoryModel
case errors.Is(err, ErrContextNotCreated), errors.Is(err, ErrContextCreationFailed):
return CategoryContext
case errors.Is(err, ErrTokenizationFailed), errors.Is(err, ErrInvalidToken):
return CategoryToken
case errors.Is(err, ErrGenerationFailed), errors.Is(err, ErrSamplingFailed):
return CategoryGeneration
case errors.Is(err, ErrOutOfMemory), errors.Is(err, ErrMemoryAllocationFailed):
return CategoryMemory
case errors.Is(err, ErrInvalidConfig), errors.Is(err, ErrConfigValidationFailed):
return CategoryConfig
case errors.Is(err, ErrBackendNotAvailable), errors.Is(err, ErrGPUNotAvailable):
return CategoryBackend
case errors.Is(err, ErrFileNotFound), errors.Is(err, ErrFileReadFailed):
return CategoryFile
case errors.Is(err, ErrInvalidParameter), errors.Is(err, ErrParameterOutOfRange):
return CategoryParameter
case errors.Is(err, ErrThreadingFailed), errors.Is(err, ErrConcurrencyViolation):
return CategoryThread
default:
return CategoryLibrary
}
}
// Global error handler instance
var globalErrorHandler = NewErrorHandler(true)
// SetGlobalErrorHandler sets the global error handler
func SetGlobalErrorHandler(handler *ErrorHandler) {
globalErrorHandler = handler
}
// GetGlobalErrorHandler returns the global error handler
func GetGlobalErrorHandler() *ErrorHandler {
return globalErrorHandler
}
// HandleError is a convenience function that uses the global error handler
func HandleError(err error, context string) error {
return globalErrorHandler.HandleError(err, context)
}
// CheckResult checks a result code and returns an appropriate error
func CheckResult(result int, function string) error {
if result == LLAMA_ERR_SUCCESS {
return nil
}
err := ErrorfromCode(result)
if llamaErr, ok := err.(*LlamaError); ok {
llamaErr.Function = function
}
return HandleError(err, function)
}
// Must panics if the error is not nil - use only for initialization
func Must(err error) {
if err != nil {
panic(fmt.Sprintf("gollama initialization failed: %v", err))
}
}
// Try executes a function and handles any panics as errors
func Try(fn func() error) (err error) {
defer func() {
if r := recover(); r != nil {
switch x := r.(type) {
case error:
err = x
case string:
err = errors.New(x)
default:
err = fmt.Errorf("unknown panic: %v", r)
}
}
}()
return fn()
}