Skip to content

Commit

Permalink
errors: Add error for interrupted conversions
Browse files Browse the repository at this point in the history
Add an error for interrupted conversions, in order to better
differentiate this scenario from other ValueErrors that may be raised
throughout the code's lifetime.
  • Loading branch information
apyrgio committed Sep 26, 2023
1 parent 0273522 commit 30196ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions dangerzone/conversion/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,15 @@ class PDFtoPPMInvalidDepth(PDFtoPPMException):
error_message = "Error converting PDF to Pixels (Invalid PPM depth)"


class InterruptedConversion(ConversionException):
"""Protocol received num of bytes different than expected"""

error_code = ERROR_SHIFT + 60
error_message = (
"Something interrupted the conversion and it could not be completed."
)


class UnexpectedConversionError(PDFtoPPMException):
error_code = ERROR_SHIFT + 100
error_message = "Some unexpected error occurred while converting the document"
Expand Down
6 changes: 3 additions & 3 deletions dangerzone/isolation_provider/qubes.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from pathlib import Path
from typing import IO, Callable, Optional

from ..conversion import errors
from ..conversion.common import calculate_timeout, running_on_qubes
from ..conversion.errors import exception_from_error_code
from ..conversion.pixels_to_pdf import PixelsToPDF
from ..document import Document
from ..util import (
Expand All @@ -41,7 +41,7 @@ def read_bytes(f: IO[bytes], size: int, timeout: float, exact: bool = True) -> b
"""Read bytes from a file-like object."""
buf = nonblocking_read(f, size, timeout)
if exact and len(buf) != size:
raise ValueError("Did not receive exact number of bytes")
raise errors.InterruptedConversion
return buf


Expand Down Expand Up @@ -126,7 +126,7 @@ def _convert(

try:
n_pages = read_int(self.proc.stdout, timeout)
except ValueError:
except errors.InterruptedConversion:
error_code = p.wait()
# XXX Reconstruct exception from error code
raise exception_from_error_code(error_code) # type: ignore [misc]
Expand Down

0 comments on commit 30196ff

Please sign in to comment.