Skip to content

✨ Add PrintInfoLevelErrorAndParamsWithDebugTransformer error handler#410

Open
glynternet wants to merge 2 commits into
masterfrom
gh/params-error-printer
Open

✨ Add PrintInfoLevelErrorAndParamsWithDebugTransformer error handler#410
glynternet wants to merge 2 commits into
masterfrom
gh/params-error-printer

Conversation

@glynternet

@glynternet glynternet commented Jan 7, 2026

Copy link
Copy Markdown

Add a cobra error handler that prints error messages along with werror params (both safe and unsafe). When debug mode is enabled, delegates to a custom transformer function instead for detailed error output.

Changes Made

  • New function PrintInfoLevelErrorAndParamsWithDebugTransformer: Cobra error handler that extracts and prints werror safe/unsafe params alongside error messages
  • New helper formattedParamLine: Formats individual param key-value pairs with JSON marshalling
  • New dependency: Added github.com/palantir/witchcraft-go-error for werror param extraction

Risk Assessment

Risk Level: Low ⚡

  • Adds new functionality without modifying existing behavior
  • All existing tests continue to pass
  • New function has comprehensive test coverage

This change is Reviewable

@changelog-app

changelog-app Bot commented Jan 7, 2026

Copy link
Copy Markdown

Generate changelog in changelog/@unreleased

Type (Select exactly one)

  • Feature (Adding new functionality)
  • Improvement (Improving existing functionality)
  • Fix (Fixing an issue with existing functionality)
  • Break (Creating a new major version by breaking public APIs)
  • Deprecation (Removing functionality in a non-breaking way)
  • Migration (Automatically moving data/functionality to a new system)

Description

✨ Add PrintInfoLevelErrorAndParamsWithDebugTransformer error handler

Check the box to generate changelog(s)

  • Generate changelog entry

@changelog-app

changelog-app Bot commented Jan 7, 2026

Copy link
Copy Markdown

Successfully generated changelog entry!

Need to regenerate?

Simply interact with the changelog bot comment again to regenerate these entries.


📋Changelog Preview

✨ Features

  • ✨ Add PrintInfoLevelErrorAndParamsWithDebugTransformer error handler (#410)

Add a cobra error handler that prints error messages along with werror
params (both safe and unsafe). When debug mode is enabled, delegates to
a custom transformer function instead for detailed error output.
@glynternet
glynternet force-pushed the gh/params-error-printer branch from 9333bc3 to e5786a2 Compare January 7, 2026 23:57

@nmiyake nmiyake left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Some feedback.

Comment thread cobracli/execute.go Outdated
})
}

// PrintInfoLevelErrorAndParamsWithDebugTransformer provides a cobra error handler that will print the error message and params as standard.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as standard.

A bit confused by this wording -- does it mean as a standard part of the operation? Or to standard out?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, I think it's worth mentioning in the documentation that the default implementation will print the JSON-marshaled representation of values, or the error encountered while trying to marshal the value as JSON

Comment thread cobracli/execute.go Outdated
Comment on lines +227 to +228
_, _ = fmt.Fprintln(os.Stdout, "Error copying error buffer to file:", err.Error())
_, _ = fmt.Fprintln(os.Stdout, "Buffer:", buf.String())

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering if it make more sense to have the function take an optional errWriter instead (and maybe default to os.Stderr if that is nil)?

It might be a bit heavy-handed to dictate that error writing to the output stream for the command will force error output to os.Stdout

Comment thread cobracli/execute.go Outdated
Comment on lines +244 to +249
for key := range safe {
keys = append(keys, key)
}
for key := range unsafe {
keys = append(keys, key)
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit, but I think this is a bit more readable:

Suggested change
for key := range safe {
keys = append(keys, key)
}
for key := range unsafe {
keys = append(keys, key)
}
keys = slices.AppendSeq(keys, maps.Keys(safe))
keys = slices.AppendSeq(keys, maps.Keys(unsafe))

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow, yeah, this code has followed me around for years, I guess I never got around to updating to modern go std pkg APIs. Thx.

Comment thread cobracli/execute.go Outdated
}
}

func formattedParamLine(key string, val interface{}) string {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func formattedParamLine(key string, val interface{}) string {
func formattedParamLine(key string, val any) string {

…Transformer

- Add errWriter io.Writer parameter (defaults to os.Stderr) instead of
  writing through command.ErrOrStderr() with an os.Stdout fallback.
- Clarify godoc: drop ambiguous "as standard" wording, document the
  JSON-marshalled value formatting and marshal-error fallback, and
  call out that this handler does not use the cobra command's stream.
- Use maps.Keys + slices.AppendSeq for param key collection.
- Use any instead of interface{}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants