show_error.go 683 B

1234567891011121314151617181920212223242526
  1. package diag
  2. import (
  3. "fmt"
  4. "io"
  5. )
  6. // ShowError shows an error. It uses the Show method if the error
  7. // implements Shower, and uses Complain to print the error message otherwise.
  8. func ShowError(w io.Writer, err error) {
  9. if shower, ok := err.(Shower); ok {
  10. fmt.Fprintln(w, shower.Show(""))
  11. } else {
  12. Complain(w, err.Error())
  13. }
  14. }
  15. // Complain prints a message to w in bold and red, adding a trailing newline.
  16. func Complain(w io.Writer, msg string) {
  17. fmt.Fprintf(w, "\033[31;1m%s\033[m\n", msg)
  18. }
  19. // Complainf is like Complain, but accepts a format string and arguments.
  20. func Complainf(w io.Writer, format string, args ...any) {
  21. Complain(w, fmt.Sprintf(format, args...))
  22. }