fix: format wrapped error chains with newlines and indentation#21384
Merged
fix: format wrapped error chains with newlines and indentation#21384
Conversation
…dability Co-authored-by: pelikhan <4175913+pelikhan@users.noreply.github.com>
Copilot created this pull request from a session on behalf of
pelikhan
March 17, 2026 12:10
View session
Contributor
There was a problem hiding this comment.
Pull request overview
Improves readability of long fmt.Errorf("%w")-wrapped error chains by rendering each unwrap layer on its own indented line (and indenting joined/multiline errors), then updates CLI call sites to use the new formatting.
Changes:
- Add
console.FormatErrorChain(err)plus helpers to unwrap and pretty-print error chains with indentation. - Switch top-level CLI error printing (and workflow compile error output) to use
FormatErrorChain. - Add unit tests for basic wrapped and joined error formatting behavior.
Reviewed changes
Copilot reviewed 4 out of 4 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| pkg/console/console.go | Adds FormatErrorChain and helpers for unwrapping/formatting multiline error chains. |
| pkg/console/console_formatting_test.go | Adds tests validating basic chain formatting, multiline output, and indentation behavior. |
| pkg/cli/add_command.go | Uses FormatErrorChain when printing compile errors. |
| cmd/gh-aw/main.go | Uses FormatErrorChain(err) for unformatted top-level errors. |
Comments suppressed due to low confidence (1)
pkg/console/console.go:324
- formatMultilineError currently drops empty continuation lines (
if line != ""), which changes the original error text for messages that intentionally include blank lines (e.g.\n\nseparators in user instructions). To avoid losing formatting, preserve empty lines by still writing the newline + indentation even whenlineis empty.
for _, line := range lines[1:] {
if line != "" {
sb.WriteString("\n ")
sb.WriteString(line)
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
You can also share your feedback on Copilot code review. Take the survey.
Comment on lines
+263
to
+274
| var sb strings.Builder | ||
| sb.WriteString(applyStyle(styles.Error, "✗ ")) | ||
| sb.WriteString(chain[0]) | ||
| for _, msg := range chain[1:] { | ||
| // Each message in the chain may itself contain newlines (e.g. from errors.Join | ||
| // nested inside a wrapping error); expand them all with consistent indentation. | ||
| for line := range strings.SplitSeq(msg, "\n") { | ||
| if line != "" { | ||
| sb.WriteString("\n ") | ||
| sb.WriteString(line) | ||
| } | ||
| } |
Comment on lines
+296
to
+304
| // If the pattern does not match, the full message is used as a fallback | ||
| // so no information is lost. | ||
| suffix := ": " + innerMsg | ||
| if strings.HasSuffix(outerMsg, suffix) { | ||
| chain = append(chain, outerMsg[:len(outerMsg)-len(suffix)]) | ||
| } else { | ||
| // Format does not follow the standard ": %w" pattern; keep the full message. | ||
| chain = append(chain, outerMsg) | ||
| } |
| err: errors.Join(errors.New("error one"), errors.New("error two")), | ||
| expectedContains: []string{"✗", "error one", "error two"}, | ||
| expectMultiLine: true, | ||
| }, |
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.
Long
fmt.Errorf-wrapped error chains are rendered as a single hard-to-read line. Each layer of context is colon-concatenated, making it difficult to identify the root cause at a glance.Changes
pkg/console/console.go— addsFormatErrorChain(err error) stringthat walkserrors.Unwrapto extract per-level messages and formats them with consistent 2-space indentation. Also handleserrors.Join-style multi-errors whose.Error()output contains newlines.cmd/gh-aw/main.go— top-level error handler now callsFormatErrorChain(err)instead ofFormatErrorMessage(errMsg)for unformatted errors.pkg/cli/add_command.go— compile-step error display usesFormatErrorChain.Before / After
Before:
After:
The suffix-stripping logic assumes the standard
fmt.Errorf("prefix: %w", inner)pattern; if the pattern doesn't match the full message is used as a fallback so no information is lost.