Skip to content

Commit cfcfc36

Browse files
committed
Fix execpton handle
1 parent f8d5255 commit cfcfc36

File tree

1 file changed

+54
-19
lines changed

1 file changed

+54
-19
lines changed

plugins/modules/zos_fetch.py

Lines changed: 54 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -314,11 +314,12 @@
314314

315315

316316
try:
317-
from zoautil_py import datasets, mvscmd, ztypes, gdgs
317+
from zoautil_py import datasets, mvscmd, ztypes, gdgs, zoau_exceptions
318318
except Exception:
319319
datasets = ZOAUImportError(traceback.format_exc())
320320
mvscmd = ZOAUImportError(traceback.format_exc())
321321
ztypes = ZOAUImportError(traceback.format_exc())
322+
zoau_exceptions = ZOAUImportError(traceback.format_exc())
322323

323324

324325
class FetchHandler:
@@ -621,7 +622,7 @@ def _fetch_pdse(self, src, is_binary, temp_dir=None, encoding=None):
621622
622623
Raises
623624
------
624-
fail_json
625+
ZOSFetchError
625626
Error copying partitioned dataset to USS.
626627
fail_json
627628
Error converting encoding of the member.
@@ -635,21 +636,23 @@ def _fetch_pdse(self, src, is_binary, temp_dir=None, encoding=None):
635636
if is_binary:
636637
copy_args["options"] = "-B"
637638

638-
rc = datasets.copy(source=src, target=dir_path, **copy_args)
639+
try:
640+
rc = datasets.copy(source=src, target=dir_path, **copy_args)
639641

640-
if rc != 0:
642+
except zoau_exceptions.ZOAUException as copy_exception:
641643
rmtree(dir_path)
642-
self._fail_json(
644+
raise ZOSFetchError(
643645
msg=(
644646
"Error copying partitioned data set {0} to USS. Make sure it is"
645647
" not empty".format(src)
646648
),
647-
stdout="",
648-
stderr="Error copying partitioned data set {0} to USS. Make sure it is not empty",
649-
stdout_lines="",
650-
stderr_lines="Error copying partitioned data set {0} to USS. Make sure it is not empty".splitlines(),
651-
rc=rc,
649+
rc=copy_exception.response.rc,
650+
stdout=copy_exception.response.stdout_response,
651+
stderr=copy_exception.response.stderr_response,
652+
stdout_lines=copy_exception.response.stdout_response.splitlines(),
653+
stderr_lines=copy_exception.response.stderr_response.splitlines(),
652654
)
655+
653656
if (not is_binary) and encoding:
654657
enc_utils = encode.EncodeUtils()
655658
from_code_set = encoding.get("from")
@@ -746,7 +749,7 @@ def _fetch_mvs_data(self, src, is_binary, temp_dir=None, file_override=None, enc
746749
747750
Raises
748751
------
749-
fail_json
752+
ZOSFetchError
750753
Unable to copy to USS.
751754
fail_json
752755
Error converting encoding of the dataset.
@@ -767,18 +770,20 @@ def _fetch_mvs_data(self, src, is_binary, temp_dir=None, file_override=None, enc
767770
if is_binary:
768771
copy_args["options"] = "-B"
769772

770-
rc = datasets.copy(source=src, target=file_path, **copy_args)
773+
try:
774+
rc = datasets.copy(source=src, target=file_path, **copy_args)
771775

772-
if rc != 0:
776+
except zoau_exceptions.ZOAUException as copy_exception:
773777
os.remove(file_path)
774-
self._fail_json(
778+
raise ZOSFetchError(
775779
msg="Unable to copy {0} to USS".format(src),
776-
stdout="",
777-
stderr="Unable to copy {0} to USS".format(src),
778-
rc=rc,
779-
stdout_lines="",
780-
stderr_lines="Unable to copy {0} to USS".format(src),
780+
rc=copy_exception.response.rc,
781+
stdout=copy_exception.response.stdout_response,
782+
stderr=copy_exception.response.stderr_response,
783+
stdout_lines=copy_exception.response.stdout_response.splitlines(),
784+
stderr_lines=copy_exception.response.stderr_response.splitlines(),
781785
)
786+
782787
if (not is_binary) and encoding:
783788
enc_utils = encode.EncodeUtils()
784789
from_code_set = encoding.get("from")
@@ -1045,6 +1050,36 @@ def run_module():
10451050
module.exit_json(**res_args)
10461051

10471052

1053+
class ZOSFetchError(Exception):
1054+
def __init__(self, msg, rc="", stdout="", stderr="", stdout_lines="", stderr_lines=""):
1055+
"""Error in a copy operation.
1056+
1057+
Parameters
1058+
----------
1059+
msg : str
1060+
Human readable string describing the exception.
1061+
rc : int
1062+
Result code.
1063+
stdout : str
1064+
Standart output.
1065+
stderr : str
1066+
Standart error.
1067+
stdout_lines : str
1068+
Standart output divided in lines.
1069+
stderr_lines : str
1070+
Standart error divided in lines.
1071+
"""
1072+
self.json_args = dict(
1073+
msg=msg,
1074+
rc=rc,
1075+
stdout=stdout,
1076+
stderr=stderr,
1077+
stdout_lines=stdout_lines,
1078+
stderr_lines= stderr_lines,
1079+
)
1080+
super().__init__(self.msg)
1081+
1082+
10481083
def main():
10491084
run_module()
10501085

0 commit comments

Comments
 (0)