Skip to content

Commit

Permalink
informative RED runtime error message when type error detected
Browse files Browse the repository at this point in the history
  • Loading branch information
lainio committed Aug 17, 2023
1 parent 4c79008 commit 1c0c9de
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 2 deletions.
86 changes: 86 additions & 0 deletions internal/color/color.go
Original file line number Diff line number Diff line change
@@ -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
}
10 changes: 8 additions & 2 deletions internal/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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())
}
}

Expand Down

0 comments on commit 1c0c9de

Please sign in to comment.