3535 secondary_training_status_changed ,
3636 secondary_training_status_message ,
3737)
38+ from sagemaker import exceptions
3839
3940LOGGER = logging .getLogger ("sagemaker" )
4041
@@ -826,10 +827,12 @@ def wait_for_model_package(self, model_package_name, poll=5):
826827
827828 if status != "Completed" :
828829 reason = desc .get ("FailureReason" , None )
829- raise ValueError (
830- "Error creating model package {}: {} Reason: {}" .format (
831- model_package_name , status , reason
832- )
830+ raise exceptions .UnexpectedStatusException (
831+ message = "Error creating model package {package}: {status} Reason: {reason}" .format (
832+ package = model_package_name , status = status , reason = reason
833+ ),
834+ allowed_statuses = ["Completed" ],
835+ actual_status = status ,
833836 )
834837 return desc
835838
@@ -990,7 +993,7 @@ def wait_for_job(self, job, poll=5):
990993 (dict): Return value from the ``DescribeTrainingJob`` API.
991994
992995 Raises:
993- ValueError : If the training job fails.
996+ exceptions.UnexpectedStatusException : If the training job fails.
994997 """
995998 desc = _wait_until_training_done (
996999 lambda last_desc : _train_done (self .sagemaker_client , job , last_desc ), None , poll
@@ -1009,7 +1012,7 @@ def wait_for_compilation_job(self, job, poll=5):
10091012 (dict): Return value from the ``DescribeCompilationJob`` API.
10101013
10111014 Raises:
1012- ValueError : If the compilation job fails.
1015+ exceptions.UnexpectedStatusException : If the compilation job fails.
10131016 """
10141017 desc = _wait_until (lambda : _compilation_job_status (self .sagemaker_client , job ), poll )
10151018 self ._check_job_status (job , desc , "CompilationJobStatus" )
@@ -1026,7 +1029,7 @@ def wait_for_tuning_job(self, job, poll=5):
10261029 (dict): Return value from the ``DescribeHyperParameterTuningJob`` API.
10271030
10281031 Raises:
1029- ValueError : If the hyperparameter tuning job fails.
1032+ exceptions.UnexpectedStatusException : If the hyperparameter tuning job fails.
10301033 """
10311034 desc = _wait_until (lambda : _tuning_job_status (self .sagemaker_client , job ), poll )
10321035 self ._check_job_status (job , desc , "HyperParameterTuningJobStatus" )
@@ -1043,23 +1046,23 @@ def wait_for_transform_job(self, job, poll=5):
10431046 (dict): Return value from the ``DescribeTransformJob`` API.
10441047
10451048 Raises:
1046- ValueError : If the transform job fails.
1049+ exceptions.UnexpectedStatusException : If the transform job fails.
10471050 """
10481051 desc = _wait_until (lambda : _transform_job_status (self .sagemaker_client , job ), poll )
10491052 self ._check_job_status (job , desc , "TransformJobStatus" )
10501053 return desc
10511054
10521055 def _check_job_status (self , job , desc , status_key_name ):
10531056 """Check to see if the job completed successfully and, if not, construct and
1054- raise a ValueError .
1057+ raise a exceptions.UnexpectedStatusException .
10551058
10561059 Args:
10571060 job (str): The name of the job to check.
10581061 desc (dict[str, str]): The result of ``describe_training_job()``.
10591062 status_key_name (str): Status key name to check for.
10601063
10611064 Raises:
1062- ValueError : If the training job fails.
1065+ exceptions.UnexpectedStatusException : If the training job fails.
10631066 """
10641067 status = desc [status_key_name ]
10651068 # If the status is capital case, then convert it to Camel case
@@ -1068,7 +1071,13 @@ def _check_job_status(self, job, desc, status_key_name):
10681071 if status not in ("Completed" , "Stopped" ):
10691072 reason = desc .get ("FailureReason" , "(No reason provided)" )
10701073 job_type = status_key_name .replace ("JobStatus" , " job" )
1071- raise ValueError ("Error for {} {}: {} Reason: {}" .format (job_type , job , status , reason ))
1074+ raise exceptions .UnexpectedStatusException (
1075+ message = "Error for {job_type} {job_name}: {status}. Reason: {reason}" .format (
1076+ job_type = job_type , job_name = job , status = status , reason = reason
1077+ ),
1078+ allowed_statuses = ["Completed" , "Stopped" ],
1079+ actual_status = status ,
1080+ )
10721081
10731082 def wait_for_endpoint (self , endpoint , poll = 5 ):
10741083 """Wait for an Amazon SageMaker endpoint deployment to complete.
@@ -1085,8 +1094,12 @@ def wait_for_endpoint(self, endpoint, poll=5):
10851094
10861095 if status != "InService" :
10871096 reason = desc .get ("FailureReason" , None )
1088- raise ValueError (
1089- "Error hosting endpoint {}: {} Reason: {}" .format (endpoint , status , reason )
1097+ raise exceptions .UnexpectedStatusException (
1098+ message = "Error hosting endpoint {endpoint}: {status}. Reason: {reason}." .format (
1099+ endpoint = endpoint , status = status , reason = reason
1100+ ),
1101+ allowed_statuses = ["InService" ],
1102+ actual_status = status ,
10901103 )
10911104 return desc
10921105
@@ -1334,7 +1347,7 @@ def logs_for_job( # noqa: C901 - suppress complexity warning for this method
13341347 completion (default: 5).
13351348
13361349 Raises:
1337- ValueError : If waiting and the training job fails.
1350+ exceptions.UnexpectedStatusException : If waiting and the training job fails.
13381351 """
13391352
13401353 description = self .sagemaker_client .describe_training_job (TrainingJobName = job_name )
0 commit comments