2018-10-11 00:20:32 +08:00
|
|
|
package diag
|
2018-10-08 19:05:40 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
2020-03-31 07:27:08 +08:00
|
|
|
// ShowError shows an error. It uses the Show method if the error
|
|
|
|
// implements Shower, and uses Complain to print the error message otherwise.
|
2020-04-15 05:35:02 +08:00
|
|
|
func ShowError(w io.Writer, err error) {
|
2020-03-31 07:27:08 +08:00
|
|
|
if shower, ok := err.(Shower); ok {
|
2020-04-15 05:35:02 +08:00
|
|
|
fmt.Fprintln(w, shower.Show(""))
|
2018-10-08 19:05:40 +08:00
|
|
|
} else {
|
2020-04-15 05:35:02 +08:00
|
|
|
Complain(w, err.Error())
|
2018-10-08 19:05:40 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-15 05:35:02 +08:00
|
|
|
// Complain prints a message to w in bold and red, adding a trailing newline.
|
|
|
|
func Complain(w io.Writer, msg string) {
|
|
|
|
fmt.Fprintf(w, "\033[31;1m%s\033[m\n", msg)
|
2018-10-08 19:05:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Complainf is like Complain, but accepts a format string and arguments.
|
2022-03-20 23:50:25 +08:00
|
|
|
func Complainf(w io.Writer, format string, args ...any) {
|
2020-04-15 05:35:02 +08:00
|
|
|
Complain(w, fmt.Sprintf(format, args...))
|
2018-10-08 19:05:40 +08:00
|
|
|
}
|