From 30196ff35b61753e62b3d93ebbcc99d608ee819a Mon Sep 17 00:00:00 2001 From: Alex Pyrgiotis Date: Tue, 26 Sep 2023 16:28:59 +0300 Subject: [PATCH] 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. --- dangerzone/conversion/errors.py | 9 +++++++++ dangerzone/isolation_provider/qubes.py | 6 +++--- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/dangerzone/conversion/errors.py b/dangerzone/conversion/errors.py index 636e308..f7c6cbb 100644 --- a/dangerzone/conversion/errors.py +++ b/dangerzone/conversion/errors.py @@ -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" diff --git a/dangerzone/isolation_provider/qubes.py b/dangerzone/isolation_provider/qubes.py index 315bf45..36ff5ec 100644 --- a/dangerzone/isolation_provider/qubes.py +++ b/dangerzone/isolation_provider/qubes.py @@ -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]