Recover from internal errors without crashing
This commit is contained in:
parent
8176e8c6f8
commit
34724b941a
2 changed files with 52 additions and 40 deletions
|
@ -11,6 +11,7 @@ import (
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
"github.com/go-errors/errors"
|
||||||
isatty "github.com/mattn/go-isatty"
|
isatty "github.com/mattn/go-isatty"
|
||||||
|
lua "github.com/yuin/gopher-lua"
|
||||||
"github.com/zyedidia/micro/internal/action"
|
"github.com/zyedidia/micro/internal/action"
|
||||||
"github.com/zyedidia/micro/internal/buffer"
|
"github.com/zyedidia/micro/internal/buffer"
|
||||||
"github.com/zyedidia/micro/internal/config"
|
"github.com/zyedidia/micro/internal/config"
|
||||||
|
@ -218,9 +219,6 @@ func main() {
|
||||||
|
|
||||||
screen.Init()
|
screen.Init()
|
||||||
|
|
||||||
// If we have an error, we can exit cleanly and not completely
|
|
||||||
// mess up the terminal being worked in
|
|
||||||
// In other words we need to shut down tcell before the program crashes
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err := recover(); err != nil {
|
if err := recover(); err != nil {
|
||||||
screen.Screen.Fini()
|
screen.Screen.Fini()
|
||||||
|
@ -284,17 +282,36 @@ func main() {
|
||||||
<-screen.DrawChan
|
<-screen.DrawChan
|
||||||
}
|
}
|
||||||
|
|
||||||
var event tcell.Event
|
|
||||||
|
|
||||||
// wait for initial resize event
|
// wait for initial resize event
|
||||||
select {
|
select {
|
||||||
case event = <-events:
|
case event := <-events:
|
||||||
action.Tabs.HandleEvent(event)
|
action.Tabs.HandleEvent(event)
|
||||||
case <-time.After(10 * time.Millisecond):
|
case <-time.After(10 * time.Millisecond):
|
||||||
// time out after 10ms
|
// time out after 10ms
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Since this loop is very slow (waits for user input every time) it's
|
||||||
|
// okay to be inefficient and run it via a function every time
|
||||||
|
// We do this so we can recover from panics without crashing the editor
|
||||||
for {
|
for {
|
||||||
|
DoEvent()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// DoEvent runs the main action loop of the editor
|
||||||
|
func DoEvent() {
|
||||||
|
var event tcell.Event
|
||||||
|
|
||||||
|
// recover from errors without crashing the editor
|
||||||
|
defer func() {
|
||||||
|
if err := recover(); err != nil {
|
||||||
|
if e, ok := err.(*lua.ApiError); ok {
|
||||||
|
screen.TermMessage("Lua API error:", e)
|
||||||
|
} else {
|
||||||
|
screen.TermMessage("Micro encountered an error:", errors.Wrap(err, 2).ErrorStack(), "\nIf you can reproduce this error, please report it at https://github.com/zyedidia/micro/issues")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
// Display everything
|
// Display everything
|
||||||
screen.Screen.Fill(' ', config.DefStyle)
|
screen.Screen.Fill(' ', config.DefStyle)
|
||||||
screen.Screen.HideCursor()
|
screen.Screen.HideCursor()
|
||||||
|
@ -306,8 +323,6 @@ func main() {
|
||||||
action.InfoBar.Display()
|
action.InfoBar.Display()
|
||||||
screen.Screen.Show()
|
screen.Screen.Show()
|
||||||
|
|
||||||
event = nil
|
|
||||||
|
|
||||||
// Check for new events
|
// Check for new events
|
||||||
select {
|
select {
|
||||||
case f := <-shell.Jobs:
|
case f := <-shell.Jobs:
|
||||||
|
@ -328,4 +343,3 @@ func main() {
|
||||||
action.Tabs.HandleEvent(event)
|
action.Tabs.HandleEvent(event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
2
go.sum
2
go.sum
|
@ -50,8 +50,6 @@ github.com/zyedidia/poller v1.0.1 h1:Tt9S3AxAjXwWGNiC2TUdRJkQDZSzCBNVQ4xXiQ7440s
|
||||||
github.com/zyedidia/poller v1.0.1/go.mod h1:vZXJOHGDcuK08GXhF6IAY0ZFd2WcgOR5DOTp84Uk5eE=
|
github.com/zyedidia/poller v1.0.1/go.mod h1:vZXJOHGDcuK08GXhF6IAY0ZFd2WcgOR5DOTp84Uk5eE=
|
||||||
github.com/zyedidia/pty v2.0.0+incompatible h1:Ou5vXL6tvjst+RV8sUFISbuKDnUJPhnpygApMFGweqw=
|
github.com/zyedidia/pty v2.0.0+incompatible h1:Ou5vXL6tvjst+RV8sUFISbuKDnUJPhnpygApMFGweqw=
|
||||||
github.com/zyedidia/pty v2.0.0+incompatible/go.mod h1:4y9l9yJZNxRa7GB/fB+mmDmGkG3CqmzLf4vUxGGotEA=
|
github.com/zyedidia/pty v2.0.0+incompatible/go.mod h1:4y9l9yJZNxRa7GB/fB+mmDmGkG3CqmzLf4vUxGGotEA=
|
||||||
github.com/zyedidia/tcell v1.4.2 h1:JWMDs6O1saINPIR5M3kNqlWJwkfnBZeZDZszEJi3BW8=
|
|
||||||
github.com/zyedidia/tcell v1.4.2/go.mod h1:HhlbMSCcGX15rFDB+Q1Lk3pKEOocsCUAQC3zhZ9sadA=
|
|
||||||
github.com/zyedidia/tcell v1.4.3 h1:s0nmxj22Jj+vyt4nVmnB6kbnwRxEay1zfS0j8IsbtfI=
|
github.com/zyedidia/tcell v1.4.3 h1:s0nmxj22Jj+vyt4nVmnB6kbnwRxEay1zfS0j8IsbtfI=
|
||||||
github.com/zyedidia/tcell v1.4.3/go.mod h1:HhlbMSCcGX15rFDB+Q1Lk3pKEOocsCUAQC3zhZ9sadA=
|
github.com/zyedidia/tcell v1.4.3/go.mod h1:HhlbMSCcGX15rFDB+Q1Lk3pKEOocsCUAQC3zhZ9sadA=
|
||||||
github.com/zyedidia/terminal v0.0.0-20180726154117-533c623e2415 h1:752dTQ5OatJ9M5ULK2+9lor+nzyZz+LYDo3WGngg3Rc=
|
github.com/zyedidia/terminal v0.0.0-20180726154117-533c623e2415 h1:752dTQ5OatJ9M5ULK2+9lor+nzyZz+LYDo3WGngg3Rc=
|
||||||
|
|
Loading…
Reference in a new issue