Improved the error handling of the isort formatting dialog. eric7

Sat, 29 Jul 2023 10:57:22 +0200

author
Detlev Offenbach <detlev@die-offenbachs.de>
date
Sat, 29 Jul 2023 10:57:22 +0200
branch
eric7
changeset 10136
ae09a4a9b0d1
parent 10135
36839e2c6945
child 10137
fe5195fba5f7

Improved the error handling of the isort formatting dialog.

src/eric7/CodeFormatting/IsortFormattingDialog.py file | annotate | diff | comparison | revisions
--- a/src/eric7/CodeFormatting/IsortFormattingDialog.py	Tue Jul 25 16:41:52 2023 +0200
+++ b/src/eric7/CodeFormatting/IsortFormattingDialog.py	Sat Jul 29 10:57:22 2023 +0200
@@ -18,6 +18,7 @@
 
 from isort import settings
 from isort.api import check_file, sort_file
+from isort.exceptions import ISortError
 from PyQt6.QtCore import QCoreApplication, Qt, pyqtSlot
 from PyQt6.QtWidgets import (
     QAbstractButton,
@@ -550,20 +551,24 @@
         @return result object
         @rtype IsortResult
         """
-        diffIO = io.StringIO() if withDiff else False
-        with open(os.devnull, "w") as devnull, contextlib.redirect_stderr(devnull):
-            ok = check_file(
-                filename,
-                show_diff=diffIO,
-                config=isortConfig,
-            )
-        if withDiff:
-            data = "" if ok else diffIO.getvalue()
-            diffIO.close()
-        else:
-            data = ""
+        try:
+            diffIO = io.StringIO() if withDiff else False
+            with open(os.devnull, "w") as devnull, contextlib.redirect_stderr(devnull):
+                ok = check_file(
+                    filename,
+                    show_diff=diffIO,
+                    config=isortConfig,
+                )
+            if withDiff:
+                data = "" if ok else diffIO.getvalue()
+                diffIO.close()
+            else:
+                data = ""
 
-        status = "unchanged" if ok else "changed"
+            status = "unchanged" if ok else "changed"
+        except ISortError as err:
+            status = "failed"
+            data = str(err)
 
         return IsortResult(status=status, filename=filename, data=data)
 
@@ -579,18 +584,23 @@
         @return result object
         @rtype IsortResult
         """
-        with open(os.devnull, "w") as devnull, contextlib.redirect_stderr(devnull):
-            ok = sort_file(
-                filename,
-                config=isortConfig,
-                ask_to_apply=False,
-                write_to_stdout=False,
-                show_diff=False,
-            )
+        try:
+            with open(os.devnull, "w") as devnull, contextlib.redirect_stderr(devnull):
+                ok = sort_file(
+                    filename,
+                    config=isortConfig,
+                    ask_to_apply=False,
+                    write_to_stdout=False,
+                    show_diff=False,
+                )
 
-        status = "changed" if ok else "unchanged"
+            status = "changed" if ok else "unchanged"
+            data = ""
+        except ISortError as err:
+            status = "failed"
+            data = str(err)
 
-        return IsortResult(status=status, filename=filename)
+        return IsortResult(status=status, filename=filename, data=data)
 
 
 @dataclass

eric ide

mercurial