theHarvester
theHarvester copied to clipboard
Replace `except Exception`s with proper error handling
Catching all errors with Exception
is enticing, but has several issues.
Properly catching the errors you expect to get:
- improves readability and maintainability
- improves error diagnostics
- avoids unexpected behavior
There are cases where you may be passing one argument to a function that requires two, like filter
that will go unnoticed while running or testing, as the error it raises just gets caught by a bare Exception
.
I want to avoid situations like this; exceptions should catch only expected behavior.
Love this idea.
First, There Another approach to solve this with the general exception, using the following code:
print(f'An exception has occurred: {e.__class__.__name__} : {url}')
will show us the exception type without specifying custom solution for every type
Second, The annoying thing about this, is there are many messages printed after exceptions caught. see the following as sample:
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=640
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=870
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=180
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=90
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=610
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=110
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=710
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=440
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=420
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=140
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=950
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=350
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=750
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=450
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=120
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=930
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=700
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=510
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=720
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=340
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=980
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=370
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=480
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=130
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=530
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=270
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=310
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=240
An exception has occurred: ContentTypeError : https://sonar.omnisint.io/all/www.edge-security.com?page=320
it's the same exception caught but from different threads. it's so annoying.
Third, There are errors that do not require continuing to work such as ServerDisconnectedError
or the availability internet connection. All it requires is to show the error and stop the rest of the work.