Preserve construction error causes without extra branches

This commit is contained in:
2026-11-16 19:44:09 +00:00
parent fcbff80ab2
commit 4939fdb283
4 changed files with 75 additions and 9 deletions

View File

@@ -215,6 +215,49 @@ func TestPublicConstructionPreservesConfigKitLoadError(t *testing.T) {
} }
} }
func TestPublicConstructionPreservesMalformedYAMLCause(t *testing.T) {
const sourceLabel = "prompts.yml"
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &configkit.Input{
Source: configkit.Bytes(sourceLabel, []byte("profiles: [\n")),
}}})
var loadErr *configkit.LoadError
if engine != nil || !errors.Is(err, promptkit.ErrInvalidConfig) || !errors.As(err, &loadErr) {
t.Fatalf("construction = (%#v, %v), want invalid config preserving LoadError", engine, err)
}
cause := errors.Unwrap(err)
var causeLoadErr *configkit.LoadError
if cause == nil || errors.Is(cause, promptkit.ErrInvalidConfig) || !errors.As(cause, &causeLoadErr) || causeLoadErr != loadErr {
t.Fatalf("construction cause = %v, want the original loading failure without a category branch", cause)
}
for _, diagnostic := range loadErr.Diagnostics() {
if diagnostic.Code != "yaml.invalid" {
continue
}
if diagnostic.Primary == nil {
t.Fatal("YAML diagnostic has no source location")
}
source, ok := loadErr.Source(diagnostic.Primary.SourceID)
if !ok || source.Label != sourceLabel {
t.Fatalf("YAML diagnostic source = %+v, found=%v, want %q", source, ok, sourceLabel)
}
return
}
t.Fatalf("loading diagnostics = %+v, want yaml.invalid", loadErr.Diagnostics())
}
func TestPublicConstructionPreservesOptionFailureCause(t *testing.T) {
path := filepath.Join(t.TempDir(), "missing.json")
engine, err := promptkit.NewEngine(promptkit.Config{}, promptkit.WithSchemaFile(path))
if engine != nil || !errors.Is(err, promptkit.ErrInvalidConfig) || !errors.Is(err, os.ErrNotExist) {
t.Fatalf("construction = (%#v, %v), want invalid config preserving the missing-file cause", engine, err)
}
cause := errors.Unwrap(err)
var pathErr *os.PathError
if cause == nil || errors.Is(cause, promptkit.ErrInvalidConfig) || !errors.As(cause, &pathErr) || pathErr.Path != path {
t.Fatalf("construction cause = %v, want the original path error without a category branch", cause)
}
}
func TestPublicConstructionRejectsSuppliedInvalidMainInput(t *testing.T) { func TestPublicConstructionRejectsSuppliedInvalidMainInput(t *testing.T) {
main := configkit.Input{} main := configkit.Input{}
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &main}}) engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &main}})

View File

@@ -241,12 +241,12 @@ func WithSchemaFile(path string) Option {
// content. A missing prompt collection is allowed. Schema documents and // content. A missing prompt collection is allowed. Schema documents and
// request artifacts remain runtime resources. // request artifacts remain runtime resources.
// //
// NewEngine returns an error matching ErrInvalidConfig for invalid // NewEngine returns an error matching ErrInvalidConfig through errors.Is for
// configuration or options, preserving a ConfigKit LoadError when loading // invalid configuration or options. The error unwraps to the original
// fails. Each constructed // construction cause, preserving a ConfigKit LoadError when loading fails.
// Engine has independent backend-capacity pools. Construction does not perform // Each constructed Engine has independent backend-capacity pools. Construction
// model requests or require credentials. A negative Config.TransientRetries // does not perform model requests or require credentials. A negative
// is invalid. // Config.TransientRetries is invalid.
func NewEngine(cfg Config, opts ...Option) (*Engine, error) { func NewEngine(cfg Config, opts ...Option) (*Engine, error) {
var options engineOptions var options engineOptions
for _, opt := range opts { for _, opt := range opts {
@@ -254,7 +254,7 @@ func NewEngine(cfg Config, opts ...Option) (*Engine, error) {
continue continue
} }
if err := opt.apply(&options); err != nil { if err := opt.apply(&options); err != nil {
return nil, fmt.Errorf("%w: %w", ErrInvalidConfig, err) return nil, newPublicOperationError(ErrInvalidConfig, err)
} }
} }
@@ -263,7 +263,7 @@ func NewEngine(cfg Config, opts ...Option) (*Engine, error) {
options.internal, options.internal,
) )
if err != nil { if err != nil {
return nil, fmt.Errorf("%w: %w", ErrInvalidConfig, err) return nil, newPublicOperationError(ErrInvalidConfig, err)
} }
return &Engine{internal: assembled}, nil return &Engine{internal: assembled}, nil
} }

View File

@@ -21,6 +21,10 @@ type publicOperationError struct {
message string message string
} }
func newPublicOperationError(owner, cause error) *publicOperationError {
return &publicOperationError{owner: owner, cause: cause, message: fmt.Sprintf("%v: %v", owner, cause)}
}
func (e *publicOperationError) Error() string { return e.message } func (e *publicOperationError) Error() string { return e.message }
func (e *publicOperationError) Unwrap() error { return e.cause } func (e *publicOperationError) Unwrap() error { return e.cause }
func (e *publicOperationError) Is(target error) bool { return errors.Is(e.owner, target) } func (e *publicOperationError) Is(target error) bool { return errors.Is(e.owner, target) }
@@ -44,7 +48,7 @@ func mapPublicError(err error) error {
} }
// The projection already retains ordinary causes; adding the original // The projection already retains ordinary causes; adding the original
// again would duplicate its entire subtree at each enclosing operation. // again would duplicate its entire subtree at each enclosing operation.
return &publicOperationError{owner: owner, cause: mapped, message: fmt.Sprintf("%v: %v", owner, mapped)} return newPublicOperationError(owner, mapped)
} }
} }
return mapUnownedPublicError(err) return mapUnownedPublicError(err)

View File

@@ -10,6 +10,25 @@ import (
"go.maximumdirect.net/promptkit/internal/usecase" "go.maximumdirect.net/promptkit/internal/usecase"
) )
func TestConstructionErrorPreservesJoinedCause(t *testing.T) {
operational := errors.New("independent operational failure")
cause := errors.Join(operational, context.Canceled)
engine, err := NewEngine(Config{}, optionFunc(func(*engineOptions) error {
return cause
}))
if engine != nil || err == nil {
t.Fatalf("construction = (%#v, %v), want failure", engine, err)
}
if unwrapped := errors.Unwrap(err); unwrapped != cause {
t.Fatalf("construction cause = %v, want the unchanged joined cause", unwrapped)
}
for _, target := range []error{ErrInvalidConfig, operational, context.Canceled} {
if !errors.Is(err, target) {
t.Errorf("construction error = %v, want identity %v", err, target)
}
}
}
func TestMapPublicErrorPreservesGenerationCancellation(t *testing.T) { func TestMapPublicErrorPreservesGenerationCancellation(t *testing.T) {
internalErr := fmt.Errorf("%w: %w", usecase.ErrLLMGenerate, context.Canceled) internalErr := fmt.Errorf("%w: %w", usecase.ErrLLMGenerate, context.Canceled)