Skip to content

Commit

Permalink
Merge branch '49-exception-handling-broken-in-python-2-7-x' into '51-…
Browse files Browse the repository at this point in the history
…new-release-0-5-1'

Resolve "Exception handling broken in Python 2.7.x"

See merge request open/stapled!33
  • Loading branch information
SnijderC committed Mar 30, 2018
2 parents 7721b53 + 76d4e03 commit 9a2cd9c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
7 changes: 5 additions & 2 deletions stapled/core/certfinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import fnmatch
import os
import stapled
import errno
from stapled.core.excepthandler import stapled_except_handle
from stapled.core.taskcontext import StapleTaskContext
from stapled.core.certmodel import CertModel
Expand Down Expand Up @@ -165,13 +166,15 @@ def _find_new_certs(self, paths, cert_path=None):
dirs = []
try:
dirs = os.listdir(path)
except NotADirectoryError:
except (OSError, IOError) as exc:
# If a path is actually a file we can still use it..
if os.path.isfile(path):
if exc.errno == errno.ENOTDIR and os.path.isfile(path):
LOG.debug("%s may be a single file", path)
# This will allow us to use our usual iteration.
dirs = [os.path.basename(path)]
path = os.path.dirname(path)
else:
raise exc
for entry in dirs:
entry = os.path.join(path, entry)
if os.path.isdir(entry):
Expand Down
14 changes: 12 additions & 2 deletions stapled/core/excepthandler.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import datetime
import logging
import os
import errno
import traceback
import configargparse
from stapled.core.exceptions import OCSPBadResponse
Expand Down Expand Up @@ -150,8 +151,17 @@ def stapled_except_handle(ctx=None):
"entries",
err_count, len_ocsp_urls
)
except PermissionError as exc:
LOG.critical(exc)
except (IOError, OSError) as exc:
if exc.errno==errno.EPERM or exc.errno==errno.EACCES:
reason = "Permission error"
elif exc.errno==errno.ENOENT:
reason = "File not found error"
elif isinstance(exc, IOError):
reason = "I/O Error"
else:
reason = "OS Error"

LOG.critical("{}: {}".format(reason, str(exc)))

# the show must go on..
except Exception as exc: # pylint: disable=broad-except
Expand Down

0 comments on commit 9a2cd9c

Please sign in to comment.