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
except FileNotFoundError as e:
print(f"Commande non trouvé : {e.filename}", file=sys.stderr)
print(
raise Exception(
"clang est requis pour utilisé ce programme, installé-le via "
f"votre gestionnaire de paquet : {e.filename}",
file=sys.stderr,
f"votre gestionnaire de paquet : {e.filename}"
)
exit(1)
return file_path, output

View file

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

View file

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