File tree Expand file tree Collapse file tree 2 files changed +19
-12
lines changed
Expand file tree Collapse file tree 2 files changed +19
-12
lines changed Original file line number Diff line number Diff line change 1+ Properly handle `subprocess.CalledProcessError `. Catch the exception, print the error, and exit with the `.returncode ` attribute value.
Original file line number Diff line number Diff line change 99import logging
1010import os
1111import pathlib
12+ import subprocess
1213import sys
1314import typing
1415from 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 ]:
You can’t perform that action at this time.
0 commit comments