From f835c8ce7f1cb90f1eb4e47d70f6a2b7c97c8046 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sat, 23 Feb 2019 20:41:52 +0900 Subject: [PATCH] uidriver/mobile: Convert a panic to an error for handling This is a dirty hack but there is no other way to handle panics to report to Crashlytics. --- internal/uidriver/mobile/ui.go | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/internal/uidriver/mobile/ui.go b/internal/uidriver/mobile/ui.go index e546f4189..f0d9b0c1a 100644 --- a/internal/uidriver/mobile/ui.go +++ b/internal/uidriver/mobile/ui.go @@ -18,7 +18,9 @@ package mobile import ( "context" + "fmt" "image" + "runtime/debug" "sync" "time" @@ -192,7 +194,17 @@ func (u *UserInterface) RunWithoutMainLoop(width, height int, scale float64, tit return ch } -func (u *UserInterface) run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics, mainloop bool) error { +func (u *UserInterface) run(width, height int, scale float64, title string, context driver.UIContext, graphics driver.Graphics, mainloop bool) (err error) { + // Convert the panic to a regular error so that Java/Objective-C layer can treat this easily e.g., for + // Crashlytics. A panic is treated as SIGABRT, and there is no way to handle this on Java/Objective-C layer + // unfortunately. + // TODO: Panic on other goroutines cannot be handled here. + defer func() { + if r := recover(); r != nil { + err = fmt.Errorf("%v\n%q", r, string(debug.Stack())) + } + }() + u.m.Lock() u.width = width u.height = height