never exit, always return errors to user

This commit is contained in:
Mylloon 2023-06-02 12:24:08 +02:00
parent ec439aff69
commit 526038c7da
Signed by: Anri
GPG key ID: A82D63DFF8D1317F
3 changed files with 24 additions and 24 deletions

View file

@ -28,12 +28,10 @@ def clang_format(file_path: str, style: str) -> tuple[str, str]:
output = e.output output = e.output
except FileNotFoundError as e: except FileNotFoundError as e:
print(f"Commande non trouvé : {e.filename}", file=sys.stderr) print(f"Commande non trouvé : {e.filename}", file=sys.stderr)
print( raise Exception(
"clang est requis pour utilisé ce programme, installé-le via " "clang est requis pour utilisé ce programme, installé-le via "
f"votre gestionnaire de paquet : {e.filename}", f"votre gestionnaire de paquet : {e.filename}"
file=sys.stderr,
) )
exit(1)
return file_path, output return file_path, output

View file

@ -1,6 +1,6 @@
import sys
from tkinter import Button, Frame, Label, Menu, Tk, Toplevel from tkinter import Button, Frame, Label, Menu, Tk, Toplevel
from tkinter.filedialog import askdirectory, askopenfile from tkinter.filedialog import askdirectory, askopenfile
from tkinter.messagebox import showerror
from src.clangformat import Style, clang_format, parse_clang_format_output from src.clangformat import Style, clang_format, parse_clang_format_output
from src.errors import ClangError from src.errors import ClangError
@ -36,8 +36,7 @@ class GUI:
], ],
) )
except AttributeError: except AttributeError:
print("Impossible de trouver le fichier", file=sys.stderr) showerror(f"Erreur - {self.name}", "Impossible de trouver le fichier")
exit(1)
else: else:
if chemin: if chemin:
self.current_location = chemin.name self.current_location = chemin.name
@ -47,8 +46,7 @@ class GUI:
try: try:
chemin = askdirectory() chemin = askdirectory()
except AttributeError: except AttributeError:
print("Impossible de trouver le dossier", file=sys.stderr) showerror(f"Erreur - {self.name}", "Impossible de trouver le dossier")
exit(1)
else: else:
self.current_location = chemin self.current_location = chemin
return self._analyse(start, chemin) return self._analyse(start, chemin)
@ -131,12 +129,21 @@ class GUI:
def _analyse(self, start_row: int, path: str) -> None: def _analyse(self, start_row: int, path: str) -> None:
"""Analyse les données""" """Analyse les données"""
files = analyze_args([path]) try:
data = [] files = analyze_args([path])
for file in files: except Exception as e:
clang_format_output = clang_format(file, self.style) showerror(f"Erreur - {self.name}", str(e))
data += parse_clang_format_output(clang_format_output) else:
self._pager(1, data, start_row) data = []
for file in files:
try:
clang_format_output = clang_format(file, self.style)
except Exception as e:
showerror(f"Erreur - {self.name}", str(e))
return
else:
data += parse_clang_format_output(clang_format_output)
self._pager(1, data, start_row)
def _reset_frame(self) -> None: def _reset_frame(self) -> None:
"""Reset frame""" """Reset frame"""

View file

@ -1,5 +1,4 @@
import os import os
import sys
exts_list = { exts_list = {
"CSharp": ["cs"], "CSharp": ["cs"],
@ -19,25 +18,21 @@ exts_list = {
def analyze_args(args: list) -> list[str]: def analyze_args(args: list) -> list[str]:
"""Renvoie la liste des fichiers""" """Renvoie la liste des fichiers"""
if len(args) > 1: if len(args) > 1:
print("Trop d'arguments renseignés.", file=sys.stderr) raise Exception("Trop d'arguments renseignés.")
exit(1)
if not len(args): if not len(args):
print("Aucun argument renseigné.") raise Exception("Aucun argument renseigné.")
exit(1)
if os.path.isdir(args[0]): if os.path.isdir(args[0]):
args = iterate_over_directory(args[0]) args = iterate_over_directory(args[0])
else: else:
if not list(filter(lambda file: os.path.exists(file), args)): if not list(filter(lambda file: os.path.exists(file), args)):
print("Le fichier n'existe pas.", file=sys.stderr) raise Exception("Le fichier n'existe pas.")
exit(1)
files = list(filter(check_ext, args)) files = list(filter(check_ext, args))
if not files: if not files:
print(f"Aucun fichiers supporté ({', '.join(args)}).", file=sys.stderr) raise Exception(f"Aucun fichiers supporté ({', '.join(args)}).")
exit(1)
return files return files