Skip to content

Commit

Permalink
Improve error classes
Browse files Browse the repository at this point in the history
 - Make 'msg' an attribute on our common error hierarchies
 - Report the error summary in the CLI even in non-verbose mode.
  • Loading branch information
MatthiasValvekens committed Sep 17, 2022
1 parent 4de7385 commit b1f0963
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 10 deletions.
11 changes: 6 additions & 5 deletions pyhanko/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,20 +101,21 @@ def pyhanko_exception_manager():
exception = e
msg = (
"Failed to read PDF file in strict mode; rerun with "
"--no-strict-syntax to try again"
"--no-strict-syntax to try again.\n"
f"Error message: {e.msg}"
)
except misc.PdfReadError as e:
exception = e
msg = "Failed to read PDF file."
msg = f"Failed to read PDF file: {e.msg}"
except misc.PdfWriteError as e:
exception = e
msg = "Failed to write PDF file."
msg = f"Failed to write PDF file: {e.msg}"
except SigningError as e:
exception = e
msg = "Error raised while producing signed file."
msg = f"Error raised while producing signed file: {e.msg}"
except LayoutError as e:
exception = e
msg = "Error raised while producing signature layout."
msg = f"Error raised while producing signature layout: {e.msg}"
except Exception as e:
exception = e
msg = "Generic processing error."
Expand Down
8 changes: 6 additions & 2 deletions pyhanko/pdf_utils/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@

class LayoutError(ValueError):
"""Indicates an error in a layout computation."""
pass

def __init__(self, msg: str, *args):
self.msg = msg
super().__init__(msg, *args)


class BoxSpecificationError(LayoutError):
"""Raised when a box constraint is over/underspecified."""
pass
def __init__(self, msg: Optional[str] = None):
super().__init__(msg=msg or "box constraint is over/underspecified")


class BoxConstraints:
Expand Down
8 changes: 6 additions & 2 deletions pyhanko/pdf_utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,10 @@ def __str__(self):


class PdfError(Exception):
pass

def __init__(self, msg: str, *args):
self.msg = msg
super().__init__(msg, *args)


class PdfReadError(PdfError):
Expand All @@ -199,7 +202,8 @@ class PdfStrictReadError(PdfReadError):


class IndirectObjectExpected(PdfReadError):
pass
def __init__(self, msg: Optional[str] = None):
super().__init__(msg=msg or "indirect object expected")


class PdfWriteError(PdfError):
Expand Down
4 changes: 3 additions & 1 deletion pyhanko/sign/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,9 @@ class SigningError(ValueError):
"""
Error encountered while signing a file.
"""
pass
def __init__(self, msg: str, *args):
self.msg = msg
super().__init__(msg, *args)


class UnacceptableSignerError(SigningError):
Expand Down

0 comments on commit b1f0963

Please sign in to comment.