feat: add structured error wrapper for controller errors#628
Draft
Yetkin Timocin (ytimocin) with Copilot wants to merge 2 commits into
Draft
feat: add structured error wrapper for controller errors#628Yetkin Timocin (ytimocin) with Copilot wants to merge 2 commits into
Yetkin Timocin (ytimocin) with Copilot wants to merge 2 commits into
Conversation
- Add pkg/utils/controller/errors.go with wrappedError, WithValues, LogAndUnwrap - Remove klog.ErrorS side-effects from NewUnexpectedBehaviorError, NewExpectedBehaviorError, NewAPIServerError, NewUserError - Fix fmt.Errorf(%w: %v) pattern that lost inner error chain - Add errors_test.go with table-driven tests for all new functionality - Update CONTRIBUTING.md with error-handling convention Refs #627 Agent-Logs-Url: https://github.com/kubefleet-dev/kubefleet/sessions/38f6461d-8fff-48b7-a1cb-5d3d76438229 Co-authored-by: ytimocin <5220939+ytimocin@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Add convention for logging errors where handled
feat: add structured error wrapper for controller errors
Apr 23, 2026
Collaborator
|
Copilot some checks are failing. Please fix. |
Contributor
Author
|
Just as a heads up, I was blocked by some firewall rules while working on your feedback. Expand below for details. Warning Firewall rules blocked me from connecting to one or more addresses (expand for details)I tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
| // wrappedError carries a sentinel error, an inner error, and accumulated klog-style | ||
| // key-value pairs. It preserves the full error chain so that errors.Is resolves both | ||
| // the sentinel and any error nested inside the inner error. | ||
| type wrappedError struct { |
Contributor
There was a problem hiding this comment.
I'd suggest moving this to its own internal package.
maybe wrap, or errwrap? unless we forsee more error related helpers, and just have it in an errors package?
errors.Wrap(err, ...)
or wrap.Err(err, ...) both read nicely.
preference for the latter as it is clearer that it's our package, and therefore has special wrapping handling.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Constructors like
NewAPIServerError/NewUnexpectedBehaviorErrorlogged viaklog.ErrorSas a side-effect while returning, causing one failure to produce multiple log lines. Thefmt.Errorf("%w: %v", Err, err.Error())pattern also brokeerrors.Isagainst the inner error chain.New API —
pkg/utils/controller/errors.gowrappedErrorUnwrap() []errorreturns both soerrors.Isresolves the full chainWithValues(err, kv...)LogAndUnwrap(err, msg)klog.ErrorSwith all accumulated kvs, returns unwrapped inner errorConstructor changes —
controller.goRemoved
klog.ErrorSside-effect from all four constructors; structured kvs previously passed to the log call are now embedded in the wrapper:NewUnexpectedBehaviorErrorklog.ErrorS(err, "Unexpected behavior...", "stackTrace", ...)"stackTrace"NewExpectedBehaviorErrorklog.ErrorS(err, "Expected behavior...")NewAPIServerErrorklog.ErrorS(err, "Error returned...", "fromCache", ..., "reason", ...)"fromCache","reason"NewUserErrorklog.ErrorS(err, "Failed to process...")Existing
Err*sentinels and their public identity are unchanged. Call sites that currently doklog.ErrorS(controller.NewXxxError(...))still compile; migration toWithValues/LogAndUnwrapis a follow-up.Convention —
CONTRIBUTING.mdAdded "Error handling convention" section: wrap with
WithValueswhile returning, emit once withLogAndUnwrapat the handler.Refs #627