From: Axy Date: Sun, 29 Mar 2026 20:07:35 +0000 (+0200) Subject: Better error handling X-Git-Url: https://git.uwuaxy.net/flexible_layout.mp4?a=commitdiff_plain;h=2aa7cbd699a2d831ecf97fd9632b781c16005ed7;p=axy%2Fft%2Fa-maze-ing.git Better error handling --- diff --git a/a_maze_ing.py b/a_maze_ing.py index 19e7789..d837464 100644 --- a/a_maze_ing.py +++ b/a_maze_ing.py @@ -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} diff --git a/mazegen/display/tty.py b/mazegen/display/tty.py index 098b2e8..f956c6b 100644 --- a/mazegen/display/tty.py +++ b/mazegen/display/tty.py @@ -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)