mirror of
https://github.com/aljazceru/opencode.git
synced 2026-01-05 08:54:55 +01:00
fix: tui panic from logger (#2075)
This commit is contained in:
@@ -2,12 +2,31 @@ package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"reflect"
|
||||
"sync"
|
||||
|
||||
opencode "github.com/sst/opencode-sdk-go"
|
||||
)
|
||||
|
||||
func sanitizeValue(val any) any {
|
||||
if val == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err, ok := val.(error); ok {
|
||||
return err.Error()
|
||||
}
|
||||
|
||||
v := reflect.ValueOf(val)
|
||||
if v.Kind() == reflect.Interface && !v.IsNil() {
|
||||
return fmt.Sprintf("%T", val)
|
||||
}
|
||||
|
||||
return val
|
||||
}
|
||||
|
||||
type APILogHandler struct {
|
||||
client *opencode.Client
|
||||
service string
|
||||
@@ -67,21 +86,13 @@ func (h *APILogHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||
h.mu.Lock()
|
||||
for _, attr := range h.attrs {
|
||||
val := attr.Value.Any()
|
||||
if err, ok := val.(error); ok {
|
||||
extra[attr.Key] = err.Error()
|
||||
} else {
|
||||
extra[attr.Key] = val
|
||||
}
|
||||
extra[attr.Key] = sanitizeValue(val)
|
||||
}
|
||||
h.mu.Unlock()
|
||||
|
||||
r.Attrs(func(attr slog.Attr) bool {
|
||||
val := attr.Value.Any()
|
||||
if err, ok := val.(error); ok {
|
||||
extra[attr.Key] = err.Error()
|
||||
} else {
|
||||
extra[attr.Key] = val
|
||||
}
|
||||
extra[attr.Key] = sanitizeValue(val)
|
||||
return true
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user