Files
GroundPY/error.py

25 lines
948 B
Python
Raw Normal View History

2025-08-31 13:48:32 +10:00
from console import console
from sys import exit
2025-09-02 15:55:21 +10:00
from typing import Union
2025-08-31 13:48:32 +10:00
2025-09-02 15:55:21 +10:00
def traceback(code: str, error_type: str, error_message: str, line: Union[int, None] = None, start_column: Union[int, None] = None, end_column: Union[int, None] = None):
2025-08-31 13:48:32 +10:00
if line != None:
console.print(f"[bold red]{error_type} on line {line}: [/]{error_message}\n")
lines = code.split("\n")[line-1:line+2]
console.print(f"{line } > " + lines[0], highlight=False)
if start_column != None and end_column != None:
console.print(" " + (" " * start_column) + "[bold red]" + ("^" * (end_column-start_column+1)))
try:
console.print(f"{line+1} " + lines[1], highlight=False)
console.print(f"{line+2} " + lines[2], highlight=False)
console.print(" ...", highlight=False)
except IndexError: # the file is less than 3 lines i guess
pass
else:
console.print(f"[bold red]{error_type}: {error_message}")
exit(1)
2025-08-31 13:48:32 +10:00