Skip to content

Commit

Permalink
feat: Handle process execution error
Browse files Browse the repository at this point in the history
  • Loading branch information
flozz committed Mar 28, 2024
1 parent 278eb00 commit 206c210
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
8 changes: 4 additions & 4 deletions scanline_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ def list_scanners(browsesecs=1, verbose=False):
:param bool verbose: Increase verbosity of scanline logs (default: ``False``).
:raise ScanlineExecutableNotFound: if the scanline app is not installed.
:raise ScanlineUnknownError: if an unexpected error occured when running
scanline.
:raise subprocess.CalledProcessError: if something goes wrong while running
the scanline command.
:rtype: list(str)
:returns: the available scanners.
Expand All @@ -132,7 +132,6 @@ def list_scanners(browsesecs=1, verbose=False):
if verbose:
command += ["-verbose"]

# TODO Handle exceptions
logger.info("Running command: %s" % " ".join(command))
proc = subprocess.run(command, check=True, capture_output=True)
logger.info(proc.stdout.decode("UTF-8", errors="ignore"))
Expand Down Expand Up @@ -190,6 +189,8 @@ def scan_flatbed(
:raise ScanlineExecutableNotFound: if the scanline app is not installed.
:raise ScanlineUnknownError: if scanline has not generated the expected
output file without returning a specific error.
:raise subprocess.CalledProcessError: if something goes wrong while running
the scanline command.
:rtype: None
"""
Expand Down Expand Up @@ -272,7 +273,6 @@ def scan_flatbed(
command += ["-name", tmp_output_path.with_suffix("").name]

# Call scanline
# TODO Handle exceptions
logger.info("Running command: %s" % " ".join(command))
proc = subprocess.run(command, check=True, capture_output=True)
logger.info(proc.stdout.decode("UTF-8", errors="ignore"))
Expand Down
3 changes: 3 additions & 0 deletions test/mock/scanline-cmd-error.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

exit 42
17 changes: 17 additions & 0 deletions test/test_wrapper.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import subprocess
from pathlib import Path

import pytest
Expand Down Expand Up @@ -66,6 +67,14 @@ def test_scanline_not_available(self, monkeypatch):
with pytest.raises(scanline_wrapper.ScanlineExecutableNotFound):
scanline_wrapper.list_scanners()

def test_scanline_unknown_error(self, monkeypatch, tmp_path):
monkeypatch.setenv(
"SCANLINE_CMD",
(Path(__file__).parent / "mock" / "scanline-cmd-error.sh").as_posix(),
)
with pytest.raises(subprocess.CalledProcessError):
scanline_wrapper.list_scanners()


class Test_scan_flatbed:

Expand Down Expand Up @@ -164,3 +173,11 @@ def test_scanline_not_available(self, monkeypatch, tmp_path):
)
with pytest.raises(scanline_wrapper.ScanlineExecutableNotFound):
scanline_wrapper.scan_flatbed(tmp_path / "out.jpg")

def test_scanline_unknown_error(self, monkeypatch, tmp_path):
monkeypatch.setenv(
"SCANLINE_CMD",
(Path(__file__).parent / "mock" / "scanline-cmd-error.sh").as_posix(),
)
with pytest.raises(subprocess.CalledProcessError):
scanline_wrapper.scan_flatbed(tmp_path / "out.jpg")

0 comments on commit 206c210

Please sign in to comment.