[gnome-games/wip/exalm/runner-refactor: 10/58] runner: Throw errors from try_init_phase_one()



commit b7b2bbd20068ecb1fc50645eda68beb4cd1e7c0a
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Fri Mar 6 15:25:11 2020 +0500

    runner: Throw errors from try_init_phase_one()
    
    Replace boolean + an out parameter with regular errors.

 src/command/command-runner.vala | 14 +++-----------
 src/core/runner.vala            |  2 +-
 src/retro/retro-runner.vala     | 26 ++++++--------------------
 src/ui/display-view.vala        | 15 +++++++++------
 4 files changed, 19 insertions(+), 38 deletions(-)
---
diff --git a/src/command/command-runner.vala b/src/command/command-runner.vala
index 17c36597..bcd07647 100644
--- a/src/command/command-runner.vala
+++ b/src/command/command-runner.vala
@@ -32,17 +32,9 @@ public class Games.CommandRunner : Object, Runner {
                this.args = args;
        }
 
-       public bool try_init_phase_one (out string error_message) {
-               if (args.length > 0) {
-                       error_message = "";
-
-                       return true;
-               }
-
-               debug ("Invalid command: it doesn’t have any argument.");
-               error_message = _("The game doesn’t have a valid command.");
-
-               return false;
+       public void try_init_phase_one () throws RunnerError {
+               if (args.length <= 0)
+                       throw new RunnerError.INVALID_GAME (_("The game doesn’t have a valid command."));
        }
 
        public Gtk.Widget get_display () {
diff --git a/src/core/runner.vala b/src/core/runner.vala
index 298aaa3e..55a934ef 100644
--- a/src/core/runner.vala
+++ b/src/core/runner.vala
@@ -15,7 +15,7 @@ public interface Games.Runner : Object {
        public abstract Gtk.Widget get_display ();
        public abstract HeaderBarWidget? get_extra_widget ();
 
-       public abstract bool try_init_phase_one (out string error_message);
+       public abstract void try_init_phase_one () throws RunnerError;
        public abstract void start () throws Error;
        public abstract void restart ();
        public abstract void resume ();
diff --git a/src/retro/retro-runner.vala b/src/retro/retro-runner.vala
index b80437d2..fbecd5eb 100644
--- a/src/retro/retro-runner.vala
+++ b/src/retro/retro-runner.vala
@@ -106,36 +106,22 @@ public class Games.RetroRunner : Object, Runner {
                deinit ();
        }
 
-       // init_phase_one attempts to init everything that can be init-ed right away
-       // It is called by the DisplayView to check if a runner can be used
-       // This method must be called before other methods/properties
-       public bool try_init_phase_one (out string error_message) {
+       public void try_init_phase_one () throws RunnerError {
                try {
                        init_phase_one ();
                // TODO: Check for the two RetroErrors using RetroCoreManager
                }
                catch (RetroError.MODULE_NOT_FOUND e) {
-                       debug (e.message);
-                       error_message = get_unsupported_system_message ();
-
-                       return false;
+                       debug ("%s\n", e.message);
+                       throw new RunnerError.UNSUPPORTED_SYSTEM (get_unsupported_system_message ());
                }
                catch (RetroError.FIRMWARE_NOT_FOUND e) {
-                       debug (e.message);
-                       error_message = get_unsupported_system_message ();
-
-                       return false;
+                       debug ("%s\n", e.message);
+                       throw new RunnerError.UNSUPPORTED_SYSTEM (get_unsupported_system_message ());
                }
                catch (Error e) {
-                       debug (e.message);
-                       error_message = e.message;
-
-                       return false;
+                       throw new RunnerError.OTHER (e.message);
                }
-
-               // Nothing went wrong
-               error_message = "";
-               return true;
        }
 
        private string get_core_id () throws Error {
diff --git a/src/ui/display-view.vala b/src/ui/display-view.vala
index 804ad9c1..be1dc7e8 100644
--- a/src/ui/display-view.vala
+++ b/src/ui/display-view.vala
@@ -359,14 +359,17 @@ private class Games.DisplayView : Object, UiView {
                        return null;
                }
 
-               string error_message;
-               if (runner.try_init_phase_one (out error_message))
-                       return runner;
+               try {
+                       runner.try_init_phase_one ();
+               }
+               catch (RunnerError e) {
+                       reset_display_page ();
+                       box.display_running_game_failed (game, e.message);
 
-               reset_display_page ();
-               box.display_running_game_failed (game, error_message);
+                       return null;
+               }
 
-               return null;
+               return runner;
        }
 
        private Gtk.ResponseType prompt_resume_with_cancellable (Cancellable cancellable) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]