Skip to content

Commit b4eeafb

Browse files
committed
Error info type also to dedicated types
1 parent fb61f62 commit b4eeafb

File tree

5 files changed

+58
-21
lines changed

5 files changed

+58
-21
lines changed

exec_helpers/_ssh_base.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
from exec_helpers import ssh_auth
4646
from exec_helpers.api import CalledProcessErrorSubClassT
4747
from exec_helpers.api import CommandT
48+
from exec_helpers.api import ErrorInfoT
4849
from exec_helpers.api import ExpectedExitCodesT
4950
from exec_helpers.api import LogMaskReT
5051
from exec_helpers.api import OptionalStdinT
@@ -988,7 +989,7 @@ def check_call( # pylint: disable=arguments-differ
988989
command: CommandT,
989990
verbose: bool = False,
990991
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
991-
error_info: "typing.Optional[str]" = None,
992+
error_info: ErrorInfoT = None,
992993
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),
993994
raise_on_err: bool = True,
994995
*,
@@ -1067,7 +1068,7 @@ def check_stderr( # pylint: disable=arguments-differ
10671068
command: CommandT,
10681069
verbose: bool = False,
10691070
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
1070-
error_info: "typing.Optional[str]" = None,
1071+
error_info: ErrorInfoT = None,
10711072
raise_on_err: bool = True,
10721073
*,
10731074
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),

exec_helpers/api.py

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"OptionalTimeoutT",
2929
"CommandT",
3030
"LogMaskReT",
31+
"ErrorInfoT",
3132
"ChRootPathSetT",
3233
"ExpectedExitCodesT",
3334
)
@@ -52,6 +53,7 @@
5253

5354
CommandT = typing.Union[str, typing.Iterable[str]]
5455
LogMaskReT = typing.Optional[str]
56+
ErrorInfoT = typing.Optional[str]
5557
ChRootPathSetT = typing.Optional[typing.Union[str, pathlib.Path]]
5658
ExpectedExitCodesT = typing.Iterable[ExitCodeT]
5759
OptionalTimeoutT = typing.Union[int, float, None]
@@ -502,7 +504,7 @@ def check_call(
502504
command: CommandT,
503505
verbose: bool = False,
504506
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
505-
error_info: "typing.Optional[str]" = None,
507+
error_info: ErrorInfoT = None,
506508
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),
507509
raise_on_err: bool = True,
508510
*,
@@ -560,6 +562,39 @@ def check_call(
560562
open_stderr=open_stderr,
561563
**kwargs,
562564
)
565+
return self._handle_exit_code(
566+
result=result,
567+
error_info=error_info,
568+
expected_codes=expected_codes,
569+
raise_on_err=raise_on_err,
570+
exception_class=exception_class,
571+
)
572+
573+
def _handle_exit_code(
574+
self,
575+
*,
576+
result: exec_result.ExecResult,
577+
error_info: ErrorInfoT,
578+
expected_codes: ExpectedExitCodesT,
579+
raise_on_err: bool,
580+
exception_class: CalledProcessErrorSubClassT,
581+
) -> exec_result.ExecResult:
582+
"""Internal check_call logic (synchronous).
583+
584+
:param result: execution result for validation
585+
:type result: exec_result.ExecResult
586+
:param error_info: optional additional error information
587+
:type error_info: typing.Optional[str]
588+
:param raise_on_err: raise `exception_class` in case of error
589+
:type raise_on_err: bool
590+
:param expected_codes: iterable expected exit codes
591+
:type expected_codes: typing.Iterable[ExitCodeT]
592+
:param exception_class: exception class for usage in case of errors (subclass of CalledProcessError)
593+
:type exception_class: CalledProcessErrorSubClassT
594+
:return: execution result
595+
:rtype: exec_result.ExecResult
596+
:raises CalledProcessErrorSubClassT: stderr presents and raise_on_err enabled
597+
"""
563598
append: str = error_info + "\n" if error_info else ""
564599
if result.exit_code not in expected_codes:
565600
message = (
@@ -576,7 +611,7 @@ def check_stderr(
576611
command: CommandT,
577612
verbose: bool = False,
578613
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
579-
error_info: "typing.Optional[str]" = None,
614+
error_info: ErrorInfoT = None,
580615
raise_on_err: bool = True,
581616
*,
582617
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),
@@ -647,8 +682,9 @@ def check_stderr(
647682

648683
def _handle_stderr(
649684
self,
685+
*,
650686
result: exec_result.ExecResult,
651-
error_info: "typing.Optional[str]",
687+
error_info: ErrorInfoT,
652688
raise_on_err: bool,
653689
expected: ExpectedExitCodesT,
654690
exception_class: CalledProcessErrorSubClassT,

exec_helpers/async_api/api.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
from exec_helpers.api import CalledProcessErrorSubClassT
3434
from exec_helpers.api import ChRootPathSetT
3535
from exec_helpers.api import CommandT
36+
from exec_helpers.api import ErrorInfoT
3637
from exec_helpers.api import ExpectedExitCodesT
3738
from exec_helpers.api import LogMaskReT
3839
from exec_helpers.api import OptionalStdinT
@@ -310,7 +311,7 @@ async def check_call( # type: ignore # pylint: disable=invalid-overridden-meth
310311
command: CommandT,
311312
verbose: bool = False,
312313
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
313-
error_info: "typing.Optional[str]" = None,
314+
error_info: ErrorInfoT = None,
314315
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),
315316
raise_on_err: bool = True,
316317
*,
@@ -366,23 +367,20 @@ async def check_call( # type: ignore # pylint: disable=invalid-overridden-meth
366367
open_stderr=open_stderr,
367368
**kwargs,
368369
)
369-
append: str = error_info + "\n" if error_info else ""
370-
if result.exit_code not in expected_codes:
371-
message = (
372-
f"{append}Command {result.cmd!r} returned exit code "
373-
f"{result.exit_code!s} while expected {expected_codes!s}"
374-
)
375-
self.logger.error(msg=message)
376-
if raise_on_err:
377-
raise exception_class(result=result, expected=expected_codes)
378-
return result
370+
return self._handle_exit_code(
371+
result=result,
372+
error_info=error_info,
373+
expected_codes=expected_codes,
374+
raise_on_err=raise_on_err,
375+
exception_class=exception_class,
376+
)
379377

380378
async def check_stderr( # type: ignore # pylint: disable=invalid-overridden-method
381379
self,
382380
command: CommandT,
383381
verbose: bool = False,
384382
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
385-
error_info: "typing.Optional[str]" = None,
383+
error_info: ErrorInfoT = None,
386384
raise_on_err: bool = True,
387385
*,
388386
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),

exec_helpers/async_api/subprocess.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from exec_helpers import subprocess
3636
from exec_helpers.api import CalledProcessErrorSubClassT
3737
from exec_helpers.api import CommandT
38+
from exec_helpers.api import ErrorInfoT
3839
from exec_helpers.api import ExpectedExitCodesT
3940
from exec_helpers.api import LogMaskReT
4041
from exec_helpers.api import OptionalTimeoutT
@@ -437,7 +438,7 @@ async def check_call( # type: ignore # pylint: disable=arguments-differ
437438
command: CommandT,
438439
verbose: bool = False,
439440
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
440-
error_info: "typing.Optional[str]" = None,
441+
error_info: ErrorInfoT = None,
441442
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),
442443
raise_on_err: bool = True,
443444
*,
@@ -516,7 +517,7 @@ async def check_stderr( # type: ignore # pylint: disable=arguments-differ
516517
command: CommandT,
517518
verbose: bool = False,
518519
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
519-
error_info: "typing.Optional[str]" = None,
520+
error_info: ErrorInfoT = None,
520521
raise_on_err: bool = True,
521522
*,
522523
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),

exec_helpers/subprocess.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from exec_helpers import proc_enums
4141
from exec_helpers.api import CalledProcessErrorSubClassT
4242
from exec_helpers.api import CommandT
43+
from exec_helpers.api import ErrorInfoT
4344
from exec_helpers.api import ExpectedExitCodesT
4445
from exec_helpers.api import LogMaskReT
4546
from exec_helpers.api import OptionalStdinT
@@ -457,7 +458,7 @@ def check_call( # pylint: disable=arguments-differ
457458
command: CommandT,
458459
verbose: bool = False,
459460
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
460-
error_info: "typing.Optional[str]" = None,
461+
error_info: ErrorInfoT = None,
461462
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),
462463
raise_on_err: bool = True,
463464
*,
@@ -536,7 +537,7 @@ def check_stderr( # pylint: disable=arguments-differ
536537
command: CommandT,
537538
verbose: bool = False,
538539
timeout: OptionalTimeoutT = constants.DEFAULT_TIMEOUT,
539-
error_info: "typing.Optional[str]" = None,
540+
error_info: ErrorInfoT = None,
540541
raise_on_err: bool = True,
541542
*,
542543
expected: ExpectedExitCodesT = (proc_enums.EXPECTED,),

0 commit comments

Comments
 (0)