diff --git a/internal/color/color.go b/internal/color/color.go new file mode 100644 index 0000000..ddcdfa6 --- /dev/null +++ b/internal/color/color.go @@ -0,0 +1,86 @@ +package color + +import "runtime" + +const ( + reset = "\033[0m" + red = "\033[31m" + green = "\033[32m" + yellow = "\033[33m" + blue = "\033[34m" + purple = "\033[35m" + cyan = "\033[36m" + gray = "\033[37m" + white = "\033[97m" +) + +var ( + isWindows bool +) + +func init() { + isWindows = runtime.GOOS == "windows" +} + +func Reset() string { + if isWindows { + return "" + } + return reset +} + +func Red() string { + if isWindows { + return "" + } + return red +} + +func Green() string { + if isWindows { + return "" + } + return green +} + +func Yellow() string { + if isWindows { + return "" + } + return yellow +} + +func Blue() string { + if isWindows { + return "" + } + return blue +} + +func Purple() string { + if isWindows { + return "" + } + return purple +} + +func Cyan() string { + if isWindows { + return "" + } + return cyan +} + +func Gray() string { + if isWindows { + return "" + } + return gray +} + +func White() string { + if isWindows { + return "" + } + return white +} diff --git a/internal/handler/handler.go b/internal/handler/handler.go index e0f146e..ce7583b 100644 --- a/internal/handler/handler.go +++ b/internal/handler/handler.go @@ -8,6 +8,7 @@ import ( "os" "runtime" + "github.com/lainio/err2/internal/color" "github.com/lainio/err2/internal/debug" fmtstore "github.com/lainio/err2/internal/formatter" "github.com/lainio/err2/internal/str" @@ -340,8 +341,13 @@ func processArg(info *Info, i int, a ...any) { info.NilHandler = NilNoop default: // we don't panic here because we can already be in recovery, but lets - // try to show an error message at least. - fmt.Fprintln(os.Stderr, "fatal error: err2.Handle: unsupported type") + // try to show an RED error message at least. + const msg = `err2 fatal error: +--- +unsupported handler function type: err2.Handle/Catch: +see 'err2/scripts/README.md' and run auto-migration scripts for your repo +---` + fmt.Fprintln(os.Stderr, color.Red()+msg+color.Reset()) } }