Preserve construction error causes without extra branches
This commit is contained in:
@@ -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) {
|
||||
main := configkit.Input{}
|
||||
engine, err := promptkit.NewEngine(promptkit.Config{Sources: promptkit.ConfigurationSources{Main: &main}})
|
||||
|
||||
16
engine.go
16
engine.go
@@ -241,12 +241,12 @@ func WithSchemaFile(path string) Option {
|
||||
// content. A missing prompt collection is allowed. Schema documents and
|
||||
// request artifacts remain runtime resources.
|
||||
//
|
||||
// NewEngine returns an error matching ErrInvalidConfig for invalid
|
||||
// configuration or options, preserving a ConfigKit LoadError when loading
|
||||
// fails. Each constructed
|
||||
// Engine has independent backend-capacity pools. Construction does not perform
|
||||
// model requests or require credentials. A negative Config.TransientRetries
|
||||
// is invalid.
|
||||
// NewEngine returns an error matching ErrInvalidConfig through errors.Is for
|
||||
// invalid configuration or options. The error unwraps to the original
|
||||
// construction cause, preserving a ConfigKit LoadError when loading fails.
|
||||
// Each constructed Engine has independent backend-capacity pools. Construction
|
||||
// does not perform model requests or require credentials. A negative
|
||||
// Config.TransientRetries is invalid.
|
||||
func NewEngine(cfg Config, opts ...Option) (*Engine, error) {
|
||||
var options engineOptions
|
||||
for _, opt := range opts {
|
||||
@@ -254,7 +254,7 @@ func NewEngine(cfg Config, opts ...Option) (*Engine, error) {
|
||||
continue
|
||||
}
|
||||
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,
|
||||
)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: %w", ErrInvalidConfig, err)
|
||||
return nil, newPublicOperationError(ErrInvalidConfig, err)
|
||||
}
|
||||
return &Engine{internal: assembled}, nil
|
||||
}
|
||||
|
||||
@@ -21,6 +21,10 @@ type publicOperationError struct {
|
||||
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) Unwrap() error { return e.cause }
|
||||
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
|
||||
// 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)
|
||||
|
||||
@@ -10,6 +10,25 @@ import (
|
||||
"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) {
|
||||
internalErr := fmt.Errorf("%w: %w", usecase.ErrLLMGenerate, context.Canceled)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user