Move error handling code into its own library

This commit is contained in:
Kovid Goyal
2025-10-12 13:51:16 +05:30
parent 6ea4bfa433
commit b627d2e4ab
14 changed files with 35 additions and 107 deletions

View File

@@ -12,6 +12,7 @@ import (
"golang.org/x/sys/unix"
"github.com/kovidgoyal/go-parallel"
"github.com/kovidgoyal/kitty/tools/tty"
"github.com/kovidgoyal/kitty/tools/utils"
"github.com/kovidgoyal/kitty/tools/utils/style"
@@ -324,18 +325,17 @@ func (self *Loop) DebugPrintln(args ...any) {
func (self *Loop) Run() (err error) {
defer func() {
if r := recover(); r != nil {
var text string
text, err = utils.Format_stacktrace_on_panic(r)
err = parallel.Format_stacktrace_on_panic(r, 1)
is_terminal := tty.IsTerminal(os.Stderr.Fd())
if is_terminal {
os.Stderr.WriteString("\x1b]\x1b\\\x1bc\x1b[H\x1b[2J") // reset terminal
}
os.Stderr.WriteString(text)
os.Stderr.WriteString(err.Error())
os.Stderr.WriteString("\n")
if is_terminal {
if term, err := tty.OpenControllingTerm(tty.SetRaw); err == nil {
defer term.RestoreAndClose()
term.DebugPrintln(text)
term.DebugPrintln(err.Error())
fmt.Println("Press any key to exit.\r")
buf := make([]byte, 16)
_, _ = term.Read(buf)
@@ -592,8 +592,7 @@ type SizedText struct {
func (self *Loop) RecoverFromPanicInGoRoutine() {
if r := recover(); r != nil {
text, err := utils.Format_stacktrace_on_panic(r)
err = fmt.Errorf("Panicked in non-main go routine\n%s\n%w", text, err)
err := parallel.Format_stacktrace_on_panic(r, 1)
// print to kitty stdout as multiple go routines might panic but only
// one panic is reported by the main loop panic_channel
if f := tty.KittyStdout(); f != nil {

View File

@@ -12,6 +12,7 @@ import (
"golang.org/x/sys/unix"
"github.com/kovidgoyal/go-parallel"
"github.com/kovidgoyal/kitty/tools/tty"
"github.com/kovidgoyal/kitty/tools/utils"
)
@@ -46,8 +47,8 @@ func read_ignoring_temporary_errors(f *tty.Term, buf []byte) (int, error) {
func read_from_tty(pipe_r *os.File, term *tty.Term, results_channel chan<- []byte, err_channel chan<- error, quit_channel <-chan byte, leftover_channel chan<- []byte) {
defer func() {
if r := recover(); r != nil {
text, _ := utils.Format_stacktrace_on_panic(r)
err_channel <- fmt.Errorf("%s", text)
err := parallel.Format_stacktrace_on_panic(r, 1)
err_channel <- err
}
}()
keep_going := true
@@ -123,7 +124,7 @@ func read_until_primary_device_attributes_response(term *tty.Term, initial_bytes
go func() {
defer func() {
if r := recover(); r != nil {
text, _ := utils.Format_stacktrace_on_panic(r)
text := parallel.Format_stacktrace_on_panic(r, 1).Error()
received <- fmt.Errorf("%s", text)
}
}()

View File

@@ -10,6 +10,7 @@ import (
"golang.org/x/sys/unix"
"github.com/kovidgoyal/go-parallel"
"github.com/kovidgoyal/kitty/tools/tty"
"github.com/kovidgoyal/kitty/tools/utils"
)
@@ -170,8 +171,7 @@ func write_to_tty(
) {
defer func() {
if r := recover(); r != nil {
text, _ := utils.Format_stacktrace_on_panic(r)
err_channel <- fmt.Errorf("%s", text)
err_channel <- parallel.Format_stacktrace_on_panic(r, 1)
}
}()
keep_going := true

View File

@@ -7,6 +7,7 @@ import (
"slices"
"strings"
"github.com/kovidgoyal/go-parallel"
"github.com/kovidgoyal/kitty/tools/utils"
)
@@ -215,12 +216,11 @@ func ScoreItems(query string, items []string, opts Options) []*Match {
ropts := resolved_options_type{
level1: []rune(opts.Level1), level2: []rune(opts.Level2), level3: []rune(opts.Level3),
}
utils.Run_in_parallel_over_range(opts.NumberOfThreads, func(start, limit int) error {
parallel.Run_in_parallel_over_range(opts.NumberOfThreads, func(start, limit int) {
w := workspace_type{}
for i := start; i < limit; i++ {
ans[i] = score_item(items[i], i, nr, &ropts, &w)
}
return nil
}, 0, len(items))
return ans
}