Centralize candidate assessment and explicit producer fallback handling
This commit is contained in:
42
internal/framework/pipeline/producer_outcome.go
Normal file
42
internal/framework/pipeline/producer_outcome.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package pipeline
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||||
)
|
||||
|
||||
// finishProducerFailure applies the resolved terminal mode without retrying
|
||||
// production or running ordinary validators against a supported fallback.
|
||||
func finishProducerFailure[T any](ctx context.Context, mode contracts.ExecutionMode, cause error, fallback func(context.Context) (T, error)) (T, contracts.ProducerOutcome, error) {
|
||||
var zero T
|
||||
if ctx == nil {
|
||||
return zero, contracts.ProducerOutcome{}, errors.New("producer context must not be nil")
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, err
|
||||
}
|
||||
if cause == nil {
|
||||
cause = errors.New("producer did not yield an accepted result")
|
||||
}
|
||||
switch mode {
|
||||
case contracts.ModeStrict:
|
||||
return zero, contracts.ProducerOutcome{}, cause
|
||||
case contracts.ModePermissive:
|
||||
if fallback == nil {
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("producer has no supported fallback: %w", cause)
|
||||
}
|
||||
value, err := fallback(ctx)
|
||||
if canceled := ctx.Err(); canceled != nil {
|
||||
return zero, contracts.ProducerOutcome{}, canceled
|
||||
}
|
||||
if err != nil {
|
||||
return zero, contracts.ProducerOutcome{}, errors.Join(cause, fmt.Errorf("construct producer fallback: %w", err))
|
||||
}
|
||||
return value, contracts.ProducerOutcome{Disposition: contracts.ProducerFallback, FallbackReason: cause.Error(), ReuseEligible: false}, nil
|
||||
default:
|
||||
return zero, contracts.ProducerOutcome{}, fmt.Errorf("unsupported producer mode %q", mode)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user