Conversation
7659a78 to
dfcf3af
Compare
There was a problem hiding this comment.
Pull Request Overview
This PR refactors error handling to separate TTY and non-TTY error handlers, allowing callers to provide different handlers for terminal and non-terminal outputs. The motivation is that the previous implementation incorrectly assumed all custom error handlers would require TTY functionality, which prevented proper error handling in tools like goreleaser that don't require terminal features.
Key changes:
- Introduces separate
TtyErrorHandlerandNonTtyErrorHandlertypes - Deprecates the original
ErrorHandlertype and related functions - Moves terminal detection logic into the execution flow rather than assuming it in custom handlers
Reviewed Changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| help.go | Adds new TTY/non-TTY error handler functions and deprecates the original handler |
| fang.go | Refactors settings to use separate TTY/non-TTY handlers and updates execution logic |
Comments suppressed due to low confidence (1)
help.go:1
- The comment is incorrect. This function sets the error handler for when output IS a TTY, not when it's NOT a TTY.
package fang
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
|
I also wonder if |
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Pull Request Overview
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
aymanbagabas
left a comment
There was a problem hiding this comment.
Why don't you keep a single error handler and check if it's a TTY in the handler?
that would be ideal, but would also require a breaking change, as currently the error handler expects the styles etc |
|
|
What's the best way to test this one? Also I'd probably call |
|
@meowgorithm you can add a no tty error handler to example/main.go: fang.WithNoTTYErrorHandler(func(w io.Writer, err error) {
fmt.Fprintf(w, "Custom no-TTY error: %v\n", err)
}),And then run it like: go run ./example/main.go nopenopenope 2>&1 | cat |
no... the err handler expects the styles. |
Right, but can't we do something like this? func DefaultErrorHandler(w io.Writer, styles Styles, err error) {
if f, ok := w.(term.File); !ok || !term.IsTerminal(f.Fd()) {
_, _ = fmt.Fprintln(w, err.Error())
return
}
_, _ = fmt.Fprintln(w, styles.ErrorHeader.String())
_, _ = fmt.Fprintln(w, styles.ErrorText.Render(err.Error()+"."))
_, _ = fmt.Fprintln(w)
if isUsageError(err) {
_, _ = fmt.Fprintln(w, lipgloss.JoinHorizontal(
lipgloss.Left,
styles.ErrorText.UnsetWidth().Render("Try"),
styles.Program.Flag.Render(" --help "),
styles.ErrorText.UnsetWidth().UnsetMargins().UnsetTransform().Render("for usage."),
))
_, _ = fmt.Fprintln(w)
}
} |
|
there's still the issue of that the user might want to do something different if output is not a tty (like we do by default in fang). btw your suggestion is similar to my initial implementation, check PR description |
|
although, triple checking now, maybe the bg color thing isn't a problem as that check is also guarded by a |
|
@aymanbagabas you are absolutely right! |
Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
|
If we're breaking the API, I would query the terminal and check IsTTY for each FD, stdin, out, and err, then store them in the context and pass the context down to handlers etc. This way you don't need to perform the checks multiple times and you only query once. |
not doing it, ended up going back to my og fix (which was also suggested by you), as it works well enough. |
the current implementation assumes the user-provided error handler will require a tty, which might not be true at al.
case in point: goreleaser. Our error handler extracts some extra information from the error and handles it, and it does not care about whether it's a terminal or not.
without this change, an error could go without being properly handled because fang decided to simply not call the error handler.
instead, I think this check should be inside fang's default error handler, which is where it was probably supposed to be anyway.