Preserve owning categories in public errors

This commit is contained in:
2026-11-05 13:45:00 +00:00
parent e881bbdcf4
commit cd373ee46b
4 changed files with 74 additions and 0 deletions

View File

@@ -17,6 +17,31 @@ func mapPublicError(err error) error {
if err == nil {
return nil
}
var operation *usecase.OperationError
if errors.As(err, &operation) && operation != nil {
owner := publicErrorFor(operation.Category)
if owner != nil {
// Project the underlying specialized error separately, retaining both
// the outer operation and the original cause for Is/As callers.
mapped := mapPublicError(operation.Cause)
if errors.Is(operation.Cause, llm.ErrTransport) {
mapped = errors.Join(ErrTransport, mapped)
}
if errors.Is(operation.Cause, llm.ErrMalformedResponse) {
mapped = errors.Join(ErrMalformedResponse, mapped)
}
var internalCapacity *usecase.CapacityError
var internalProvider *llm.ProviderHTTPError
if errors.As(operation.Cause, &internalCapacity) || errors.As(operation.Cause, &internalProvider) {
return errors.Join(owner, mapped)
}
return errors.Join(owner, mapped, operation.Cause)
}
}
return mapUnownedPublicError(err)
}
func mapUnownedPublicError(err error) error {
var internalCapacityError *usecase.CapacityError
if errors.As(err, &internalCapacityError) && internalCapacityError != nil &&
strings.TrimSpace(internalCapacityError.BackendID) != "" {