refactor: replace fmt.Sprintf in errors with Join/Concat
All error message string building now uses core string primitives. Remaining fmt usage: code generation (%q quoting) and log formatting (%v). Co-Authored-By: Virgil <virgil@lethean.io>
This commit is contained in:
parent
5d67088080
commit
cb16b63b19
5 changed files with 20 additions and 19 deletions
|
|
@ -70,11 +70,11 @@ func GetAsset(group, name string) (string, error) {
|
|||
g, ok := assetGroups[group]
|
||||
assetGroupsMu.RUnlock()
|
||||
if !ok {
|
||||
return "", E("core.GetAsset", fmt.Sprintf("asset group %q not found", group), nil)
|
||||
return "", E("core.GetAsset", Join(" ", "asset group", group, "not found"), nil)
|
||||
}
|
||||
data, ok := g.assets[name]
|
||||
if !ok {
|
||||
return "", E("core.GetAsset", fmt.Sprintf("asset %q not found in group %q", name, group), nil)
|
||||
return "", E("core.GetAsset", Join(" ", "asset", name, "not found in group", group), nil)
|
||||
}
|
||||
return decompress(data)
|
||||
}
|
||||
|
|
@ -158,7 +158,7 @@ func ScanAssets(filenames []string) ([]ScannedPackage, error) {
|
|||
}
|
||||
fullPath, err := filepath.Abs(filepath.Join(baseDir, group, path))
|
||||
if err != nil {
|
||||
scanErr = Wrap(err, "core.ScanAssets", fmt.Sprintf("could not determine absolute path for asset %q in group %q", path, group))
|
||||
scanErr = Wrap(err, "core.ScanAssets", Join(" ", "could not determine absolute path for asset", path, "in group", group))
|
||||
return false
|
||||
}
|
||||
pkg.Assets = append(pkg.Assets, AssetRef{
|
||||
|
|
@ -176,7 +176,7 @@ func ScanAssets(filenames []string) ([]ScannedPackage, error) {
|
|||
path := strings.Trim(lit.Value, "\"")
|
||||
fullPath, err := filepath.Abs(filepath.Join(baseDir, path))
|
||||
if err != nil {
|
||||
scanErr = Wrap(err, "core.ScanAssets", fmt.Sprintf("could not determine absolute path for group %q", path))
|
||||
scanErr = Wrap(err, "core.ScanAssets", Join(" ", "could not determine absolute path for group", path))
|
||||
return false
|
||||
}
|
||||
pkg.Groups = append(pkg.Groups, fullPath)
|
||||
|
|
@ -219,7 +219,7 @@ func GeneratePack(pkg ScannedPackage) (string, error) {
|
|||
for _, groupPath := range pkg.Groups {
|
||||
files, err := getAllFiles(groupPath)
|
||||
if err != nil {
|
||||
return "", Wrap(err, "core.GeneratePack", fmt.Sprintf("failed to scan asset group %q", groupPath))
|
||||
return "", Wrap(err, "core.GeneratePack", Join(" ", "failed to scan asset group", groupPath))
|
||||
}
|
||||
for _, file := range files {
|
||||
if packed[file] {
|
||||
|
|
@ -227,12 +227,12 @@ func GeneratePack(pkg ScannedPackage) (string, error) {
|
|||
}
|
||||
data, err := compressFile(file)
|
||||
if err != nil {
|
||||
return "", Wrap(err, "core.GeneratePack", fmt.Sprintf("failed to compress asset %q in group %q", file, groupPath))
|
||||
return "", Wrap(err, "core.GeneratePack", Join(" ", "failed to compress asset", file, "in group", groupPath))
|
||||
}
|
||||
localPath := strings.TrimPrefix(file, groupPath+"/")
|
||||
relGroup, err := filepath.Rel(pkg.BaseDir, groupPath)
|
||||
if err != nil {
|
||||
return "", Wrap(err, "core.GeneratePack", fmt.Sprintf("could not determine relative path for group %q (base %q)", groupPath, pkg.BaseDir))
|
||||
return "", Wrap(err, "core.GeneratePack", Join(" ", "could not determine relative path for group", groupPath, "(base", Concat(pkg.BaseDir, ")")))
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("\tcore.AddAsset(%q, %q, %q)\n", relGroup, localPath, data))
|
||||
packed[file] = true
|
||||
|
|
@ -246,7 +246,7 @@ func GeneratePack(pkg ScannedPackage) (string, error) {
|
|||
}
|
||||
data, err := compressFile(asset.FullPath)
|
||||
if err != nil {
|
||||
return "", Wrap(err, "core.GeneratePack", fmt.Sprintf("failed to compress asset %q", asset.FullPath))
|
||||
return "", Wrap(err, "core.GeneratePack", Join(" ", "failed to compress asset", asset.FullPath))
|
||||
}
|
||||
b.WriteString(fmt.Sprintf("\tcore.AddAsset(%q, %q, %q)\n", asset.Group, asset.Name, data))
|
||||
packed[asset.FullPath] = true
|
||||
|
|
|
|||
|
|
@ -47,14 +47,14 @@ func (e *Err) Error() string {
|
|||
}
|
||||
if e.Err != nil {
|
||||
if e.Code != "" {
|
||||
return fmt.Sprintf("%s%s [%s]: %v", prefix, e.Msg, e.Code, e.Err)
|
||||
return Concat(prefix, e.Msg, " [", e.Code, "]: ", e.Err.Error())
|
||||
}
|
||||
return fmt.Sprintf("%s%s: %v", prefix, e.Msg, e.Err)
|
||||
return Concat(prefix, e.Msg, ": ", e.Err.Error())
|
||||
}
|
||||
if e.Code != "" {
|
||||
return fmt.Sprintf("%s%s [%s]", prefix, e.Msg, e.Code)
|
||||
return Concat(prefix, e.Msg, " [", e.Code, "]")
|
||||
}
|
||||
return fmt.Sprintf("%s%s", prefix, e.Msg)
|
||||
return Concat(prefix, e.Msg)
|
||||
}
|
||||
|
||||
// Unwrap returns the underlying error for use with errors.Is and errors.As.
|
||||
|
|
|
|||
|
|
@ -25,8 +25,8 @@ func NewServiceRuntime[T any](c *Core, opts T) *ServiceRuntime[T] {
|
|||
return &ServiceRuntime[T]{core: c, opts: opts}
|
||||
}
|
||||
|
||||
func (r *ServiceRuntime[T]) Core() *Core { return r.core }
|
||||
func (r *ServiceRuntime[T]) Opts() T { return r.opts }
|
||||
func (r *ServiceRuntime[T]) Core() *Core { return r.core }
|
||||
func (r *ServiceRuntime[T]) Opts() T { return r.opts }
|
||||
func (r *ServiceRuntime[T]) Config() *Config { return r.core.Config() }
|
||||
|
||||
// --- Lifecycle ---
|
||||
|
|
|
|||
|
|
@ -13,7 +13,7 @@
|
|||
|
||||
package core
|
||||
|
||||
import "fmt"
|
||||
// No imports needed — uses package-level string helpers.
|
||||
|
||||
// Service is a managed component with optional lifecycle.
|
||||
type Service struct {
|
||||
|
|
@ -60,7 +60,7 @@ func (c *Core) Service(name string, service ...Service) Result {
|
|||
return Result{Value: E("core.Service", Concat("service \"", name, "\" not permitted — registry locked"), nil)}
|
||||
}
|
||||
if _, exists := c.services.services[name]; exists {
|
||||
return Result{Value: E("core.Service", fmt.Sprintf("service %q already registered", name), nil)}
|
||||
return Result{Value: E("core.Service", Join(" ", "service", name, "already registered"), nil)}
|
||||
}
|
||||
|
||||
srv := &service[0]
|
||||
|
|
|
|||
|
|
@ -5,8 +5,9 @@
|
|||
package core
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// TaskState holds background task state.
|
||||
|
|
@ -22,7 +23,7 @@ func (c *Core) PerformAsync(t Task) string {
|
|||
if c.shutdown.Load() {
|
||||
return ""
|
||||
}
|
||||
taskID := fmt.Sprintf("task-%d", c.taskIDCounter.Add(1))
|
||||
taskID := Concat("task-", strconv.FormatUint(c.taskIDCounter.Add(1), 10))
|
||||
if tid, ok := t.(TaskWithID); ok {
|
||||
tid.SetTaskID(taskID)
|
||||
}
|
||||
|
|
@ -30,7 +31,7 @@ func (c *Core) PerformAsync(t Task) string {
|
|||
c.wg.Go(func() {
|
||||
result, handled, err := c.PERFORM(t)
|
||||
if !handled && err == nil {
|
||||
err = E("core.PerformAsync", fmt.Sprintf("no handler found for task type %T", t), nil)
|
||||
err = E("core.PerformAsync", Join(" ", "no handler found for task type", reflect.TypeOf(t).String()), nil)
|
||||
}
|
||||
_ = c.ACTION(ActionTaskCompleted{TaskID: taskID, Task: t, Result: result, Error: err})
|
||||
})
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue