]> Untitled Git - axy/ft/a-maze-ing.git/commitdiff
Better error handling
authorAxy <gilliardmarthey.axel@gmail.com>
Sun, 29 Mar 2026 20:07:35 +0000 (22:07 +0200)
committerAxy <gilliardmarthey.axel@gmail.com>
Sun, 29 Mar 2026 20:07:35 +0000 (22:07 +0200)
a_maze_ing.py
mazegen/display/tty.py

index 19e7789a65d4712c5ae7862c71b2898d3ba8c4ae..d83746492d4d657cbb6d0f3364986f6d5c91592c 100644 (file)
@@ -1,5 +1,8 @@
+from sys import stderr
+from typing import Never
 from mazegen.config.parser_combinator import ParseError
 from mazegen.display.observer import MazeRegenerate, TTYTracker
+from mazegen.display.tty import BackendException
 from mazegen.maze import (
     Maze,
     Pattern,
@@ -9,17 +12,26 @@ from mazegen.maze import (
     make_pacman,
     make_perfect,
 )
-from mazegen.config.config_parser import Config
+from mazegen.config.config_parser import Config, ConfigError
 from mazegen.maze.output import format_output
 import random
 
 config_filename = "./example.conf"
 config_str = open(config_filename).read()
+
+
+def error(s: str) -> Never:
+    print("Error:", file=stderr)
+    print(s, end="", file=stderr)
+    exit(1)
+
+
 try:
     config = Config.parse(config_str)
 except ParseError as e:
-    print(e.pretty_format(config_str, config_filename))
-    exit(1)
+    error(e.pretty_format(config_str, config_filename))
+except ConfigError as e:
+    error(e.args[0] + "\n")
 
 if config.seed is not None:
     random.seed(config.seed)
@@ -28,7 +40,10 @@ maze = Maze(config)
 
 pacman_tracker = PacmanTracker(maze)
 network_tracker = NetworkTracker(maze)
-tty_tracker = TTYTracker(maze, config) if config.visual else None
+try:
+    tty_tracker = TTYTracker(maze, config) if config.visual else None
+except BackendException as e:
+    error(e.args[0] + "\n")
 
 excluded = {maze.entry, maze.exit}
 
index 098b2e8d3816b64bf3d18f47315b3d53c6d7ea1d..f956c6b8cb51d8e2e0439e13b758af7c51bc106a 100644 (file)
@@ -358,8 +358,9 @@ def extract_pairs(
     """
     Extracts the color pairs from the config, and maps them to text
     attributes
-    May raise a backend exception if too many colors are used or an invalid
-    variable color is used
+    May raise a backend exception if too many colors are used, an invalid
+    variable color is used, or the terminal doesn't support setting colors
+    but custom colors were used
     """
     all_tilemaps = [
         e
@@ -397,6 +398,12 @@ def extract_pairs(
             raise BackendException("Unknown color " + var_color + " in config")
         res_colors[var_color] = color_lookup[var_color]
         available_colors -= {color_lookup[var_color]}
+    if len(value_colors) != 0 and not curses.can_change_color():
+        raise BackendException(
+            "Cannot set colors for terminal, "
+            + "please check that your terminal is configured correctly "
+            + "or only use named colors in config"
+        )
     if len(available_colors) < len(value_colors):
         raise BackendException(
             "Too many value color values in config: "
@@ -539,8 +546,15 @@ class TTYBackend:
             self.__dims * IVec2.splat(2) + IVec2.splat(1)
         )
 
-        self.__uninit: bool = False
-        self.__screen: curses.window = curses.initscr()
+        self.__uninit: bool = True
+        try:
+            self.__screen: curses.window = curses.initscr()
+        except curses.error as e:
+            raise BackendException(
+                "Failed to initiate screen, "
+                + "check that your terminal is setup correctly"
+            )
+        self.__uninit = False
         self.__screen.timeout(0)
         curses.start_color()
         curses.noecho()
@@ -548,7 +562,11 @@ class TTYBackend:
         curses.curs_set(0)
         self.__screen.keypad(True)
 
-        pair_map = extract_pairs(config)
+        try:
+            pair_map = extract_pairs(config)
+        except BackendException as e:
+            self.uninit()
+            raise e
         self.tilemaps = TileMaps(config, pair_map, self)
 
         self.__scratch: curses.window = curses.newpad(1, 1)