Files
ConfigKit/diagnostic.go

58 lines
1.5 KiB
Go
Raw Normal View History

package configkit
import (
"io"
2026-09-12 16:21:17 +00:00
"gitea.maximumdirect.net/eric/ConfigKit/internal/engine"
)
// LoadError reports safe structured diagnostics for a failed configuration load.
// It intentionally does not expose or unwrap parser and application errors.
2026-09-12 16:21:17 +00:00
type LoadError struct{ value *engine.LoadError }
// Error implements error without rendering diagnostic messages or input values.
func (e *LoadError) Error() string {
2026-09-12 16:21:17 +00:00
if e == nil || e.value == nil {
return "configkit: configuration load failed"
}
2026-09-12 16:21:17 +00:00
return e.value.Error()
}
// Diagnostics returns a deep copy of the error's canonical diagnostics.
func (e *LoadError) Diagnostics() []Diagnostic {
2026-09-12 16:21:17 +00:00
if e == nil || e.value == nil {
return nil
}
2026-09-12 16:21:17 +00:00
values := e.value.Diagnostics()
if values == nil {
return nil
}
2026-09-12 16:21:17 +00:00
result := make([]Diagnostic, len(values))
for i := range values {
result[i] = fromEngineDiagnostic(values[i])
}
2026-09-12 16:21:17 +00:00
return result
}
// Sources returns a deep copy of sources discovered before loading failed.
2026-09-12 16:21:17 +00:00
func (e *LoadError) Sources() []SourceInfo {
if e == nil || e.value == nil {
return nil
}
2026-09-12 16:21:17 +00:00
values := e.value.Sources()
if values == nil {
return nil
}
2026-09-12 16:21:17 +00:00
result := make([]SourceInfo, len(values))
for i := range values {
result[i] = fromEngineSourceInfo(values[i])
}
2026-09-12 16:21:17 +00:00
return result
}
// WriteDiagnostics writes diagnostics in canonical order. It escapes terminal
// control characters and propagates writer errors, including short writes.
2026-09-12 16:21:17 +00:00
func WriteDiagnostics(output io.Writer, diagnostics []Diagnostic) error {
return engine.WriteDiagnostics(output, toEngineDiagnostics(diagnostics))
}