about summary refs log tree commit diff stats
path: root/app.lua
diff options
context:
space:
mode:
authorKartik K. Agaram <vc@akkartik.com>2023-09-04 00:33:27 -0700
committerKartik K. Agaram <vc@akkartik.com>2023-09-04 13:07:53 -0700
commitadfe94b9fcf04cc52032467d8970aaf9cd4ea6dc (patch)
tree1360277317df838e5105eca6ac67ef41b3b2a3e2 /app.lua
parent99495355b4bde77cdfea18698de1472d9e83872a (diff)
downloadlines.love-adfe94b9fcf04cc52032467d8970aaf9cd4ea6dc.tar.gz
switch to source editor on error
If we're already in source editor we'll quit as before.

It's ugly that app.lua now knows about run.lua. But it's a start.
Diffstat (limited to 'app.lua')
-rw-r--r--app.lua21
1 files changed, 18 insertions, 3 deletions
diff --git a/app.lua b/app.lua
index da43889..b520077 100644
--- a/app.lua
+++ b/app.lua
@@ -2,6 +2,7 @@
 --
 -- Most apps can just use the default shown in https://love2d.org/wiki/love.run,
 -- but we need to override it to:
+--   * recover from errors (by switching to the source editor)
 --   * run all tests (functions starting with 'test_') on startup, and
 --   * save some state that makes it possible to switch between the main app
 --     and a source editor, while giving each the illusion of complete
@@ -24,22 +25,36 @@ function love.run()
             return a or 0
           end
         end
-        love.handlers[name](a,b,c,d,e,f)
+        xpcall(function() love.handlers[name](a,b,c,d,e,f) end, handle_error)
       end
     end
 
     dt = love.timer.step()
-    App.update(dt)
+    xpcall(function() App.update(dt) end, handle_error)
 
     love.graphics.origin()
     love.graphics.clear(love.graphics.getBackgroundColor())
-    App.draw()
+    xpcall(App.draw, handle_error)
     love.graphics.present()
 
     love.timer.sleep(0.001)
   end
 end
 
+function handle_error(err)
+  Error_message = debug.traceback('Error: ' .. tostring(err), --[[stack frame]]2):gsub('\n[^\n]+$', '')
+  print(Error_message)
+  if Current_app == 'run' then
+    Settings.current_app = 'source'
+    love.filesystem.write('config', json.encode(Settings))
+    load_file_from_source_or_save_directory('main.lua')
+    App.undo_initialize()
+    App.run_tests_and_initialize()
+  else
+    love.event.quit()
+  end
+end
+
 -- The rest of this file wraps around various LÖVE primitives to support
 -- automated tests. Often tests will run with a fake version of a primitive
 -- that redirects to the real love.* version once we're done with tests.