Directly raising exception from 'Exception' instance

Aperta
#321 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
45/100
Tipo di issue
Refactoring
Chiarezza
Abbastanza chiara
Stato di attività
Ferma
Stack tecnologico
python
Ambito
cloud

Direzione di ricerca

Four bare raise Exception(...) sites are named: b2sdk/sync/action.py (around line 244), b2sdk/v1/download_dest.py (get_bytes_written), and two in b2sdk/v1/sync/folder.py (the except branches for NotADirectory and UnableToCreateDirectory). Read the existing exception classes in the package first, since the issue asks for a more specific built-in or custom exception but does not choose one. Done means each site raises a specific type, the existing tests in the sync and download suites still pass, and callers that catch the old generic exception are checked.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

OpenRefactory introduces fixers, that automatically fix safety, security, reliability and compliance issues. We ran OpenRefactory's Intelligent Code Repair (iCR) analysis engine on your code. We are reporting a few sample fixes that were generated.

Throwing overly broad exceptions promotes complex error handling code that is more likely to contain security vulnerabilities. It will become challenging to catch only specific types of exceptions. The best practice is to catch only exceptions which require specific handling.

Raising Exception in a function will lead to having an except Exception and the only way to differentiate between different exceptions is by comparing the exception messages. Moreover, one could forget to re-raise some exceptions which are unintentionally caught.

OpenRefactory’s Intelligent Code Repair (iCR) for Python, identified four such cases. The diffs are the following:

--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/sync/action.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/sync/action.py
@@ -241,6 +241,8 @@
             except OSError:
                 pass
         if not os.path.isdir(parent_dir):
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('could not create directory %s' % (parent_dir,))
 
     def do_action(self, bucket, reporter):
--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/download_dest.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/download_dest.py
@@ -175,6 +175,8 @@
 
     def get_bytes_written(self):
         if self.bytes_written is None:
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('data not written yet')
         return self.bytes_written
--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
@@ -22,6 +22,8 @@
         try:
             return func(*a, **kw)
         except exception.NotADirectory as ex:
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('%s is not a directory' % (ex.path,))
         except exception.UnableToCreateDirectory as ex:
             raise Exception('unable to create directory %s' % (ex.path,))
--- /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
+++ /home/nhasan/ORTest/PythonTest/b2-sdk-python/b2sdk/v1/sync/folder.py
@@ -24,6 +24,8 @@
         except exception.NotADirectory as ex:
             raise Exception('%s is not a directory' % (ex.path,))
         except exception.UnableToCreateDirectory as ex:
+            # OpenRefactory Warning: Raising 'Exception' and 'BaseException' directly will have a negative impact on any code trying to catch these exceptions.
+            # Raise a more specific built-in exception or, create a custom one.
             raise Exception('unable to create directory %s' % (ex.path,))
         except exception.EmptyDirectory as ex:
             raise exception.CommandError(

Reference: CWE-397: Declaration of Throws for Generic Exception


Reported by OpenRefactory’s Intelligent Code Repair (iCR) for Python v1.0. More info at: https://www.openrefactory.com/

Lingua principale
Python
Stelle
214
Fork
68
Merge medio
51m
PR unite (30g)
6

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Backblaze/b2-sdk-python

Tutte le issue di Backblaze/b2-sdk-python

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.