Files
seaweedfs/seaweed-block/core/rpc/answer/failure.go
T

122 lines
3.3 KiB
Go

package answer
import (
"context"
"errors"
"fmt"
"google.golang.org/grpc/status"
)
// ErrLocalCredential identifies credential validation before a dial. It is
// independent of diagnostic text, including an underlying file-load error.
var ErrLocalCredential = errors.New("local credentials")
var ErrLocalConfiguration = errors.New("local configuration")
func LocalCredential(err error) error {
if err == nil {
return nil
}
if errors.Is(err, ErrLocalCredential) {
return err
}
return Record(Unsent, fmt.Errorf("%w: %w", ErrLocalCredential, err))
}
func LocalConfiguration(err error) error {
if err == nil {
return nil
}
if errors.Is(err, ErrLocalConfiguration) {
return err
}
return Record(Unsent, fmt.Errorf("%w: %w", ErrLocalConfiguration, err))
}
type failure struct {
phase Phase
kind Kind
cause error
}
func (f *failure) Error() string { return f.cause.Error() }
func (f *failure) Unwrap() error { return f.cause }
func (f *failure) GRPCStatus() *status.Status {
server, ok := status.FromError(f.cause)
if !ok {
return nil
} // transport evidence never manufactures a server answer
return server
}
// Record attaches the evidence of one transport attempt without deciding
// its outcome. An unlisted kind remains unlisted at PolicyOf.
func Record(phase Phase, err error) error {
if err == nil {
return nil
}
kind, _ := ErrorKind(err)
return &failure{phase: phase, kind: kind, cause: err}
}
// EnsureEvidence preserves recorded facts. An opaque RPC error without
// transport evidence can only receive the caller's conservative phase.
func EnsureEvidence(phase Phase, err error) error {
if err == nil {
return nil
}
var existing *failure
if errors.As(err, &existing) {
return err
}
return Record(phase, err)
}
// Rekind translates an owned boundary's error provenance without replacing
// the request's admission evidence. Missing evidence remains conservative.
func Rekind(err error, kind Kind) error {
if err == nil {
return nil
}
phase := PossiblyAdmitted
var recorded *failure
if errors.As(err, &recorded) {
phase = recorded.phase
}
return &failure{phase: phase, kind: kind, cause: err}
}
// PolicyOf reads evidence preserved by a transport adapter. Unannotated
// errors are not evidence that an operation was never admitted.
func PolicyOf(err error, contracts ...Contract) (Policy, error) {
var f *failure
if errors.As(err, &f) {
kind := f.kind
if len(contracts) == 1 {
kind = ForContract(contracts[0], kind)
}
if len(contracts) > 1 {
return Policy{}, fmt.Errorf("answer: multiple wire contracts")
}
return Lookup(f.phase, kind)
}
return Policy{}, fmt.Errorf("answer: failure has no admission evidence: %v", err)
}
// Snapshot one owned context once. A non-nil winning cause is immutable;
// only then read Err to distinguish a custom deadline cause from cancellation.
func RecordContext(ctx context.Context, phase Phase, err error) error {
if err == nil {
return nil
}
kind, _ := ErrorKind(err)
if cause := context.Cause(ctx); cause != nil && (kind == Connection || kind == PlaintextConnection || kind == Timeout || kind == Canceled) {
kind = Canceled
if ctx.Err() == context.DeadlineExceeded {
kind = CallerDeadline
}
return &failure{phase: phase, kind: kind, cause: errors.Join(err, ctx.Err(), cause)}
}
return Record(phase, err)
}