mirror of
https://github.com/sst/opencode.git
synced 2025-08-22 14:04:07 +00:00
fix: tui panic from logger
This commit is contained in:
parent
863ae6fa7d
commit
48976bde4b
1 changed files with 21 additions and 10 deletions
|
@ -2,12 +2,31 @@ package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"reflect"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
opencode "github.com/sst/opencode-sdk-go"
|
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 {
|
type APILogHandler struct {
|
||||||
client *opencode.Client
|
client *opencode.Client
|
||||||
service string
|
service string
|
||||||
|
@ -67,21 +86,13 @@ func (h *APILogHandler) Handle(ctx context.Context, r slog.Record) error {
|
||||||
h.mu.Lock()
|
h.mu.Lock()
|
||||||
for _, attr := range h.attrs {
|
for _, attr := range h.attrs {
|
||||||
val := attr.Value.Any()
|
val := attr.Value.Any()
|
||||||
if err, ok := val.(error); ok {
|
extra[attr.Key] = sanitizeValue(val)
|
||||||
extra[attr.Key] = err.Error()
|
|
||||||
} else {
|
|
||||||
extra[attr.Key] = val
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
h.mu.Unlock()
|
h.mu.Unlock()
|
||||||
|
|
||||||
r.Attrs(func(attr slog.Attr) bool {
|
r.Attrs(func(attr slog.Attr) bool {
|
||||||
val := attr.Value.Any()
|
val := attr.Value.Any()
|
||||||
if err, ok := val.(error); ok {
|
extra[attr.Key] = sanitizeValue(val)
|
||||||
extra[attr.Key] = err.Error()
|
|
||||||
} else {
|
|
||||||
extra[attr.Key] = val
|
|
||||||
}
|
|
||||||
return true
|
return true
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue