errors: Add error for interrupted conversions

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.
This commit is contained in:
Alex Pyrgiotis 2023-09-26 16:28:59 +03:00
parent 0273522fb1
commit 30196ff35b
No known key found for this signature in database
GPG key ID: B6C15EBA0357C9AA
2 changed files with 12 additions and 3 deletions

View file

@ -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"

View file

@ -13,8 +13,8 @@ import zipfile
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 (
@ -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
@ -126,7 +126,7 @@ class Qubes(IsolationProvider):
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]