Skip to content

Commit 2d98c80

Browse files
committed
feat: move error package to Garcon project
1 parent aa5c55a commit 2d98c80

17 files changed

Lines changed: 72 additions & 275 deletions

.dockerignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
*
22
!conf
33
!event
4-
!gie
54
!go.mod
65
!go.sum
76
!goinfer.go

README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,6 @@ groups:
370370
- GUFF files discovery: `Search()` in [`models.go`](go/conf/models.go)
371371
- Graceful shutdown handling: `handleShutdown()` in [`goinfer.go`](go/goinfer.go)
372372
- API-key authentication per service: `configureAPIKeyAuth()` in [`router.go`](go/infer/router.go)
373-
- Comprehensive error handling: `gie` package in [`errors.go`](go/gie/errors.go)
374373
375374
## API endpoints
376375

conf/conf.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515
"syscall"
1616

17-
"github.com/LynxAIeu/goinfer/gie"
17+
"github.com/LynxAIeu/garcon/gerr"
1818
"github.com/LynxAIeu/goinfer/proxy/config"
1919
"github.com/goccy/go-yaml"
2020
)
@@ -148,26 +148,26 @@ func (cfg *Cfg) validate(noAPIKey bool) error {
148148
for dir := range strings.SplitSeq(cfg.ModelsDir, ":") {
149149
info, er := os.Stat(dir)
150150
if errors.Is(er, fs.ErrNotExist) {
151-
return gie.New(gie.ConfigErr, "Verify GI_MODELS_DIR or 'models_dir' in "+GoinferINI, "does not exist", dir)
151+
return gerr.New(gerr.ConfigErr, "Verify GI_MODELS_DIR or 'models_dir' in "+GoinferINI, "does not exist", dir)
152152
}
153153
if er != nil {
154-
return gie.Wrap(er, gie.ConfigErr, "Verify GI_MODELS_DIR or 'models_dir' in "+GoinferINI, "problem with", dir)
154+
return gerr.Wrap(er, gerr.ConfigErr, "Verify GI_MODELS_DIR or 'models_dir' in "+GoinferINI, "problem with", dir)
155155
}
156156
if !info.IsDir() {
157-
return gie.New(gie.ConfigErr, "Verify GI_MODELS_DIR or 'models_dir' in "+GoinferINI, "must be a directory", dir)
157+
return gerr.New(gerr.ConfigErr, "Verify GI_MODELS_DIR or 'models_dir' in "+GoinferINI, "must be a directory", dir)
158158
}
159159
}
160160

161161
// GI_LLAMA_EXE
162162
info, err := os.Stat(cfg.Llama.Exe)
163163
if errors.Is(err, fs.ErrNotExist) {
164-
return gie.New(gie.ConfigErr, "GI_LLAMA_EXE or 'exe' in goinfer.ini: file does not exist", "exe", cfg.Llama.Exe)
164+
return gerr.New(gerr.ConfigErr, "GI_LLAMA_EXE or 'exe' in goinfer.ini: file does not exist", "exe", cfg.Llama.Exe)
165165
}
166166
if err != nil {
167-
return gie.Wrap(err, gie.ConfigErr, "GI_MODELS_DIR or 'models_dir' in goinfer.ini", "exe", cfg.Llama.Exe)
167+
return gerr.Wrap(err, gerr.ConfigErr, "GI_MODELS_DIR or 'models_dir' in goinfer.ini", "exe", cfg.Llama.Exe)
168168
}
169169
if info.IsDir() {
170-
return gie.New(gie.ConfigErr, "GI_LLAMA_EXE or 'exe' in goinfer.ini: must be a file, not a directory", "exe", cfg.Llama.Exe)
170+
return gerr.New(gerr.ConfigErr, "GI_LLAMA_EXE or 'exe' in goinfer.ini: must be a file, not a directory", "exe", cfg.Llama.Exe)
171171
}
172172

173173
// API key
@@ -176,7 +176,7 @@ func (cfg *Cfg) validate(noAPIKey bool) error {
176176
return nil
177177
}
178178
if cfg.APIKey == "" || strings.Contains(cfg.APIKey, "Please") {
179-
return gie.New(gie.ConfigErr, "API key not set, please set your private API key")
179+
return gerr.New(gerr.ConfigErr, "API key not set, please set your private API key")
180180
}
181181
if cfg.APIKey == debugAPIKey {
182182
slog.Warn("API key is DEBUG => security threat")
@@ -197,7 +197,7 @@ func (cfg *Cfg) validateAddr() error {
197197
if slices.Contains(badPorts, port) {
198198
const msg = "Chrome/Firefox block the bad ports"
199199
slog.Error(msg, "port", port, "reference", "https://fetch.spec.whatwg.org/#port-blocking")
200-
return gie.New(gie.ConfigErr, msg, "port", port, "reference", "https://fetch.spec.whatwg.org/#port-blocking")
200+
return gerr.New(gerr.ConfigErr, msg, "port", port, "reference", "https://fetch.spec.whatwg.org/#port-blocking")
201201
}
202202
return nil
203203
}

conf/files.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
"strings"
1515
"unicode"
1616

17-
"github.com/LynxAIeu/goinfer/gie"
17+
"github.com/LynxAIeu/garcon/gerr"
1818
)
1919

2020
type Root struct {
@@ -43,7 +43,7 @@ func (r *Root) RelativePath(fullPath string) (string, error) {
4343
return "", err
4444
}
4545
if !filepath.IsLocal(relativePath) {
46-
return "", gie.New(gie.Invalid, "not local", "full", fullPath, "root", r.Path)
46+
return "", gerr.New(gerr.Invalid, "not local", "full", fullPath, "root", r.Path)
4747
}
4848
return filepath.Clean(relativePath), err
4949
}
@@ -433,7 +433,7 @@ func (cfg *Cfg) ValidateSwap() error {
433433
if len(cfg.Swap.Models) == 0 {
434434
n := len(cfg.getInfo())
435435
if n == 0 {
436-
return gie.New(gie.ConfigErr, "No *.gguf files found", "dir", cfg.ModelsDir)
436+
return gerr.New(gerr.ConfigErr, "No *.gguf files found", "dir", cfg.ModelsDir)
437437
}
438438
slog.Warn("No model configured => Restart Goinfer to refresh llama-swap.yml", "models", n)
439439
return nil
@@ -477,7 +477,7 @@ func verify(root Root, path string) (int64, error) {
477477
// is empty?
478478
size := info.Size()
479479
if size < 1000 {
480-
return 0, gie.New(gie.ConfigErr, "Model file is empty (or too small)", "path", path, "size", size)
480+
return 0, gerr.New(gerr.ConfigErr, "Model file is empty (or too small)", "path", path, "size", size)
481481
}
482482

483483
// Check if the file is readable
@@ -503,7 +503,7 @@ func verify(root Root, path string) (int64, error) {
503503

504504
if path[pos-len(first):pos] != first {
505505
slog.Debug("KO Model file is part of a series, but only the first one is referenced", "path", path)
506-
return 0, gie.New(gie.ConfigErr, "Model file is part of a series, but only the first one is referenced, file="+path)
506+
return 0, gerr.New(gerr.ConfigErr, "Model file is part of a series, but only the first one is referenced, file="+path)
507507
}
508508

509509
slog.Debug("OK Model file is the first of a series", "path", path)

conf/goinferini.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
"syscall"
1717
"unsafe"
1818

19-
"github.com/LynxAIeu/goinfer/gie"
19+
"github.com/LynxAIeu/garcon/gerr"
2020
"github.com/pelletier/go-toml/v2"
2121
)
2222

@@ -28,7 +28,7 @@ const GoinferINI = "goinfer.ini"
2828
func ReadGoinferINI(noAPIKey bool, extra, start string) (*Cfg, error) {
2929
data, err := os.ReadFile(GoinferINI)
3030
if err != nil {
31-
err = gie.Wrap(err, gie.ConfigErr, "Cannot read", "file", GoinferINI)
31+
err = gerr.Wrap(err, gerr.ConfigErr, "Cannot read", "file", GoinferINI)
3232
slog.Warn("Skip " + GoinferINI + " => Use default settings and env. vars")
3333
}
3434

@@ -112,7 +112,7 @@ func (cfg *Cfg) genGoinferINI(debug, noAPIKey bool) ([]byte, error) {
112112

113113
data, er := toml.Marshal(cfg)
114114
if er != nil {
115-
er = gie.Wrap(err, gie.ConfigErr, "failed to toml.Marshal", "cfg", cfg)
115+
er = gerr.Wrap(err, gerr.ConfigErr, "failed to toml.Marshal", "cfg", cfg)
116116
if err != nil {
117117
return data, errors.Join(err, er)
118118
}
@@ -124,12 +124,12 @@ func (cfg *Cfg) genGoinferINI(debug, noAPIKey bool) ([]byte, error) {
124124
// load the configuration file (if filename not empty).
125125
func (cfg *Cfg) parse(fileData []byte) error {
126126
if len(fileData) == 0 {
127-
return gie.New(gie.ConfigErr, "empty", "file", GoinferINI)
127+
return gerr.New(gerr.ConfigErr, "empty", "file", GoinferINI)
128128
}
129129

130130
err := toml.Unmarshal(fileData, &cfg)
131131
if err != nil {
132-
return gie.Wrap(err, gie.ConfigErr, "Failed to toml.Unmarshal", "invalid TOML", string(fileData))
132+
return gerr.Wrap(err, gerr.ConfigErr, "Failed to toml.Unmarshal", "invalid TOML", string(fileData))
133133
}
134134

135135
return nil
@@ -241,13 +241,13 @@ func writeWithHeader(path, header string, body []byte) (bool, error) {
241241
_ = os.Remove(backup) // removing it because write protected
242242
err = os.Rename(path, backup)
243243
if err != nil {
244-
return false, gie.Wrap(err, gie.ConfigErr, "failed to backup", "file", path, "backup", backup)
244+
return false, gerr.Wrap(err, gerr.ConfigErr, "failed to backup", "file", path, "backup", backup)
245245
}
246246
}
247247

248248
err = os.WriteFile(path, data, 0o400) // read only
249249
if err != nil {
250-
return false, gie.Wrap(err, gie.ConfigErr, "failed to write", "file", path)
250+
return false, gerr.Wrap(err, gerr.ConfigErr, "failed to write", "file", path)
251251
}
252252

253253
return true, nil

conf/info.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"path/filepath"
1313
"strings"
1414

15-
"github.com/LynxAIeu/goinfer/gie"
15+
"github.com/LynxAIeu/garcon/gerr"
1616
)
1717

1818
type (
@@ -203,9 +203,9 @@ func (cfg *Cfg) search(params map[string]ModelParams, shells *[]*ModelInfo, root
203203
switch {
204204
case err != nil:
205205
if dir == nil {
206-
return gie.Wrap(err, gie.NotFound, "filepath.WalkDir")
206+
return gerr.Wrap(err, gerr.NotFound, "filepath.WalkDir")
207207
}
208-
return gie.Wrap(err, gie.NotFound, "filepath.WalkDir", "dir", dir.Name())
208+
return gerr.Wrap(err, gerr.NotFound, "filepath.WalkDir", "dir", dir.Name())
209209
case dir.IsDir():
210210
// => step into this directory
211211
case filepath.Base(path) == paramsYML:
@@ -227,7 +227,7 @@ func (cfg *Cfg) search(params map[string]ModelParams, shells *[]*ModelInfo, root
227227
func keepParams(params map[string]ModelParams, root, path string) error {
228228
data, err := os.ReadFile(filepath.Clean(path))
229229
if err != nil {
230-
return gie.Wrap(err, gie.ConfigErr, "os.ReadFile", "file", path)
230+
return gerr.Wrap(err, gerr.ConfigErr, "os.ReadFile", "file", path)
231231
}
232232

233233
if len(data) == 0 {
@@ -240,7 +240,7 @@ func keepParams(params map[string]ModelParams, root, path string) error {
240240
var mp map[string]ModelParams
241241
err = json.Unmarshal(data, &mp)
242242
if err != nil {
243-
return gie.Wrap(err, gie.ConfigErr, "json.Unmarshal", "file", path, "100FirsBytes", string(data[:100]))
243+
return gerr.Wrap(err, gerr.ConfigErr, "json.Unmarshal", "file", path, "100FirsBytes", string(data[:100]))
244244
}
245245

246246
for name, p := range mp {

conf/llamaswapyml.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"reflect"
1313
"strings"
1414

15-
"github.com/LynxAIeu/goinfer/gie"
15+
"github.com/LynxAIeu/garcon/gerr"
1616
"github.com/LynxAIeu/goinfer/proxy/config"
1717
"github.com/goccy/go-yaml"
1818
)
@@ -98,7 +98,7 @@ func (cfg *Cfg) GenLlamaSwapYAML(verbose, debug bool) ([]byte, error) {
9898

9999
yml, er := yaml.Marshal(&cfg.Swap)
100100
if er != nil {
101-
return nil, gie.Wrap(er, gie.ConfigErr, "failed to marshal the llama-swap config")
101+
return nil, gerr.Wrap(er, gerr.ConfigErr, "failed to marshal the llama-swap config")
102102
}
103103

104104
return yml, nil

0 commit comments

Comments
 (0)