Skip to content

Commit a56712a

Browse files
committed
Properly handle subprocess.CalledProcessError
Fixes #21 Signed-off-by: Pedro Algarvio <[email protected]>
1 parent 112bac6 commit a56712a

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

changelog/21.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Properly handle `subprocess.CalledProcessError`. Catch the exception, print the error, and exit with the `.returncode` attribute value.

src/ptscripts/parser.py

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import logging
1010
import os
1111
import pathlib
12+
import subprocess
1213
import sys
1314
import typing
1415
from collections.abc import Iterator
@@ -178,14 +179,24 @@ def run(
178179
capture: bool = False,
179180
interactive: bool = False,
180181
**kwargs,
181-
) -> CompletedProcess[str]:
182+
) -> CompletedProcess[str] | None:
182183
"""
183184
Run a subprocess.
184185
185186
Either in a virtualenv context if one was configured or the system context.
186187
"""
187-
if self.venv:
188-
return self.venv.run(
188+
try:
189+
if self.venv:
190+
return self.venv.run(
191+
*cmdline,
192+
check=check,
193+
timeout_secs=timeout_secs,
194+
no_output_timeout_secs=no_output_timeout_secs,
195+
capture=capture,
196+
interactive=interactive,
197+
**kwargs,
198+
)
199+
return self._run(
189200
*cmdline,
190201
check=check,
191202
timeout_secs=timeout_secs,
@@ -194,15 +205,10 @@ def run(
194205
interactive=interactive,
195206
**kwargs,
196207
)
197-
return self._run(
198-
*cmdline,
199-
check=check,
200-
timeout_secs=timeout_secs,
201-
no_output_timeout_secs=no_output_timeout_secs,
202-
capture=capture,
203-
interactive=interactive,
204-
**kwargs,
205-
)
208+
except subprocess.CalledProcessError as exc:
209+
self.error(str(exc))
210+
self.exit(exc.returncode)
211+
return None
206212

207213
@contextmanager
208214
def chdir(self, path: pathlib.Path) -> Iterator[pathlib.Path]:

0 commit comments

Comments
 (0)