Repository: saltstack/salt
Pull request: Clean up bare exceptions
### What does this PR do?

Clean up bare exceptions. Log exceptions that are overly broad, narrow down others.

### What issues does this PR fix or reference?
Partially resolves: #60781


### Merge requirements satisfied?
**[NOTICE] Bug fixes or features added to Salt require tests.**
<!-- Please review the [test documentation](https://docs.saltproject.io/en/master/topics/tutorials/writing_tests.html) for details on how to implement tests into Salt's test suite. -->
- [ ] Docs
- [ ] Changelog - https://docs.saltproject.io/en/master/topics/development/changelog.html
- [ ] Tests written/updated


Problem statement:
[TECH DEBT] Clean up bare/broad exception handling
### Description of the tech debt to be addressed, include links and screenshots

Clean up exception handling of bare exceptions and/or overly broad exceptions..

Things like this:
```
try:
   ...
except Exception:
   pass
````

Should be excepting a specific exception if we know what we are trying to handle. Otherwise we should at least be logging what went wrong so we can more easily narrow down the scope of the exception in the future.

```
try:
   ...
except Exception as exc:
   log.error(
       "Something went wrong: %s",
        exc, exc_info_on_loglevel=logging.DEBUG,
    )
```

Hints from issue discussion:

Commit message: Clean up bare exceptions

Patch to review:
diff --git a/salt/client/ssh/__init__.py b/salt/client/ssh/__init__.py
index d31e1952838c..1fae655d50df 100644
--- a/salt/client/ssh/__init__.py
+++ b/salt/client/ssh/__init__.py
@@ -11,6 +11,7 @@
 import logging
 import multiprocessing
 import os
+import queue
 import re
 import subprocess
 import sys
@@ -614,11 +615,7 @@ def handle_ssh(self, mine=False):
                 if "id" in ret:
                     returned.add(ret["id"])
                     yield {ret["id"]: ret["ret"]}
-            except Exception:  # pylint: disable=broad-except
-                # This bare exception is here to catch spurious exceptions
-                # thrown by que.get during healthy operation. Please do not
-                # worry about this bare exception, it is entirely here to
-                # control program flow.
+            except queue.Empty:
                 pass
             for host in running:
                 if not running[host]["thread"].is_alive():
@@ -631,7 +628,7 @@ def handle_ssh(self, mine=False):
                                 if "id" in ret:
                                     returned.add(ret["id"])
                                     yield {ret["id"]: ret["ret"]}
-                        except Exception:  # pylint: disable=broad-except
+                        except queue.Empty:
                             pass
 
                         if host not in returned:
diff --git a/salt/fileserver/roots.py b/salt/fileserver/roots.py
index 54dda2d9ee5d..bff601dba535 100644
--- a/salt/fileserver/roots.py
+++ b/salt/fileserver/roots.py
@@ -74,8 +74,8 @@ def _add_file_stat(fnd):
         """
         try:
             fnd["stat"] = list(os.stat(fnd["path"]))
-        except Exception:  # pylint: disable=broad-except
-            pass
+        except Exception as exc:  # pylint: disable=broad-except
+            log.error("Unable to stat file: %s", exc)
         return fnd
 
     if "index" in kwargs:
@@ -363,11 +363,8 @@ def _translate_sep(path):
                 try:
                     if not os.listdir(abs_path):
                         ret["empty_dirs"].add(rel_path)
-                except Exception:  # pylint: disable=broad-except
-                    # Generic exception because running os.listdir() on a
-                    # non-directory path raises an OSError on *NIX and a
-                    # WindowsError on Windows.
-                    pass
+                except OSError:
+                    log.debug("Unable to list dir: %s", abs_path)
                 if is_link:
                     link_dest = salt.utils.path.readlink(abs_path)
                     log.trace(
diff --git a/salt/loader/lazy.py b/salt/loader/lazy.py
index b3501add7033..0ee6afc03d36 100644
--- a/salt/loader/lazy.py
+++ b/salt/loader/lazy.py
@@ -771,7 +771,7 @@ def _load_module(self, name):
         except SystemExit as error:
             try:
                 fn_, _, caller, _ = traceback.extract_tb(sys.exc_info()[2])[-1]
-            except Exception:  # pylint: disable=broad-except
+            except IndexError:  # pylint: disable=broad-except
                 pass
             else:
                 tgt_fns = [
diff --git a/salt/utils/event.py b/salt/utils/event.py
index a6337cff5c65..a9111966119e 100644
--- a/salt/utils/event.py
+++ b/salt/utils/event.py
@@ -52,6 +52,7 @@
 import atexit
 import contextlib
 import datetime
+import errno
 import fnmatch
 import hashlib
 import logging
@@ -367,8 +368,18 @@ def connect_pub(self, timeout=None):
                 try:
                     self.subscriber.connect(timeout=timeout)
                     self.cpub = True
-                except Exception:  # pylint: disable=broad-except
-                    pass
+                except tornado.iostream.StreamClosedError:
+                    log.error("Encountered StreamClosedException")
+                except OSError as exc:
+                    if exc.errno != errno.ENOENT:
+                        raise
+                    log.error("Error opening stream, file does not exist")
+                except Exception as exc:  # pylint: disable=broad-except
+                    log.info(
+                        "An exception occurred connecting publisher: %s",
+                        exc,
+                        exc_info_on_loglevel=logging.DEBUG,
+                    )
         else:
             if self.subscriber is None:
                 self.subscriber = salt.transport.ipc.IPCMessageSubscriber(
@@ -412,8 +423,12 @@ def connect_pull(self, timeout=1):
                 try:
                     self.pusher.connect(timeout=timeout)
                     self.cpush = True
-                except Exception:  # pylint: disable=broad-except
-                    pass
+                except Exception as exc:  # pylint: disable=broad-except
+                    log.error(
+                        "Unable to connect pusher: %s",
+                        exc,
+                        exc_info_on_loglevel=logging.DEBUG,
+                    )
         else:
             if self.pusher is None:
                 self.pusher = salt.transport.ipc.IPCMessageClient(
@@ -816,8 +831,12 @@ def fire_event(self, data, tag, timeout=1000):
             with salt.utils.asynchronous.current_ioloop(self.io_loop):
                 try:
                     self.pusher.send(msg)
-                except Exception as ex:  # pylint: disable=broad-except
-                    log.debug(ex)
+                except Exception as exc:  # pylint: disable=broad-except
+                    log.debug(
+                        "Publisher send failed with exception: %s",
+                        ex,
+                        exc_info_on_loglevel=logging.DEBUG,
+                    )
                     raise
         else:
             self.io_loop.spawn_callback(self.pusher.send, msg)
@@ -886,7 +905,12 @@ def _fire_ret_load_specific_fun(self, load, fun_index=0):
                         data,
                         tagify([load["jid"], "sub", load["id"], "error", fun], "job"),
                     )
-        except Exception:  # pylint: disable=broad-except
+        except Exception as exc:  # pylint: disable=broad-except
+            log.error(
+                "Event iteration failed with exception: %s",
+                exc,
+                exc_info_on_loglevel=logging.DEBUG,
+            )
             pass
 
     def fire_ret_load(self, load):
diff --git a/salt/utils/files.py b/salt/utils/files.py
index cb0a6f2e25b1..1cf636a7533d 100644
--- a/salt/utils/files.py
+++ b/salt/utils/files.py
@@ -55,8 +55,12 @@ def __clean_tmp(tmp):
     """
     try:
         rm_rf(tmp)
-    except Exception:  # pylint: disable=broad-except
-        pass
+    except Exception as exc:  # pylint: disable=broad-except
+        log.error(
+            "Exception while removing temp directory: %s",
+            exc,
+            exc_info_on_loglevel=logging.DEBUG,
+        )
 
 
 def guess_archive_type(name):


Code review comments:
diff_hunk: @@ -771,7 +771,7 @@ def _load_module(self, name):
         except SystemExit as error:
             try:
                 fn_, _, caller, _ = traceback.extract_tb(sys.exc_info()[2])[-1]
-            except Exception:  # pylint: disable=broad-except
+            except IndexError:  # pylint: disable=broad-except
path: salt/loader/lazy.py
text: @user1:
```suggestion
            except IndexError:
```

diff_hunk: @@ -816,8 +831,12 @@ def fire_event(self, data, tag, timeout=1000):
             with salt.utils.asynchronous.current_ioloop(self.io_loop):
                 try:
                     self.pusher.send(msg)
-                except Exception as ex:  # pylint: disable=broad-except
-                    log.debug(ex)
+                except Exception as exc:  # pylint: disable=broad-except
+                    log.debug(
+                        "Publisher send failed with exception: %s",
+                        ex,
path: salt/utils/event.py
text: @user1:
```suggestion
                        exc,
```