Skip to content

Commit 815953e

Browse files
fix: improve error messages for waiter timeouts (aws#5785)
* fix: improve error messages for waiter timeouts * fix: re-introduce accidentially removed field * fix: apply fix to generated code as well * Update autogen scripts for adding message as parameter * Fix typo in resources.py --------- Co-authored-by: Molly He <mollyhe@amazon.com>
1 parent 709aa32 commit 815953e

8 files changed

Lines changed: 144 additions & 95 deletions

File tree

‎sagemaker-core/src/sagemaker/core/resources.py‎

Lines changed: 86 additions & 82 deletions
Large diffs are not rendered by default.

‎sagemaker-core/src/sagemaker/core/tools/constants.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@
2222

2323
RESOURCE_WITH_LOGS = set(["TrainingJob", "ProcessingJob", "TransformJob"])
2424

25+
DEFAULT_TIMEOUT_MESSAGE = "Increase the timeout and try again."
26+
RESOURCE_TIMEOUT_MESSAGES = {
27+
"TrainingJob": "Your training job is still running. Call .refresh() to check its current status.",
28+
}
29+
2530
CONFIGURABLE_ATTRIBUTE_SUBSTRINGS = [
2631
"kms",
2732
"s3",

‎sagemaker-core/src/sagemaker/core/tools/resources_codegen.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030
PYTHON_TYPES_TO_BASIC_JSON_TYPES,
3131
CONFIGURABLE_ATTRIBUTE_SUBSTRINGS,
3232
RESOURCE_WITH_LOGS,
33+
DEFAULT_TIMEOUT_MESSAGE,
34+
RESOURCE_TIMEOUT_MESSAGES,
3335
)
3436
from sagemaker.core.tools.method import Method, MethodType
3537
from sagemaker.core.utils.utils import (
@@ -1742,6 +1744,7 @@ def generate_wait_method(self, resource_name: str) -> str:
17421744
logs_arg_doc=logs_arg_doc,
17431745
init_wait_logs=init_wait_logs,
17441746
print_wait_logs=print_wait_logs,
1747+
timeout_message=RESOURCE_TIMEOUT_MESSAGES.get(resource_name, DEFAULT_TIMEOUT_MESSAGE),
17451748
)
17461749
return formatted_method
17471750

‎sagemaker-core/src/sagemaker/core/tools/templates.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def wait(
332332
return
333333
334334
if timeout is not None and time.time() - start_time >= timeout:
335-
raise TimeoutExceededError(resouce_type="{resource_name}", status=current_status)
335+
raise TimeoutExceededError(resource_type="{resource_name}", status=current_status, message="{timeout_message}")
336336
time.sleep(poll)
337337
'''
338338

@@ -385,7 +385,7 @@ def wait_for_status(
385385
return
386386
{failed_error_block}
387387
if timeout is not None and time.time() - start_time >= timeout:
388-
raise TimeoutExceededError(resouce_type="{resource_name}", status=current_status)
388+
raise TimeoutExceededError(resource_type="{resource_name}", status=current_status)
389389
time.sleep(poll)
390390
'''
391391

@@ -436,7 +436,7 @@ def wait_for_delete(
436436
{deleted_status_check}
437437
438438
if timeout is not None and time.time() - start_time >= timeout:
439-
raise TimeoutExceededError(resouce_type="{resource_name}", status=current_status)
439+
raise TimeoutExceededError(resource_type="{resource_name}", status=current_status)
440440
except botocore.exceptions.ClientError as e:
441441
error_code = e.response["Error"]["Code"]
442442

‎sagemaker-core/src/sagemaker/core/utils/exceptions.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,16 +79,17 @@ def __init__(self, resource_type="(Unkown)", reason="(Unkown)"):
7979
class TimeoutExceededError(WaiterError):
8080
"""Raised when a specified timeout is exceeded"""
8181

82-
fmt = "Timeout exceeded while waiting for {resource_type}. Final Resource State: {status}. Increase the timeout and try again."
82+
fmt = "Timeout exceeded while waiting for {resource_type}. Final Resource State: {status}. {message}"
8383

84-
def __init__(self, resource_type="(Unkown)", status="(Unkown)", reason="(Unkown)"):
84+
def __init__(self, resource_type="(Unkown)", status="(Unkown)", reason="(Unkown)", message="Increase the timeout and try again."):
8585
"""Initialize a TimeoutExceededError exception.
8686
Args:
8787
resource_type (str): The type of resource being waited on.
8888
status (str): The final status of the resource.
8989
reason (str): The reason the resource entered a failed state.
90+
message (str): Additional context or guidance for the user.
9091
"""
91-
super().__init__(resource_type=resource_type, status=status, reason=reason)
92+
super().__init__(resource_type=resource_type, status=status, reason=reason, message=message)
9293

9394

9495
### Intelligent Defaults Errors
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from sagemaker.core.utils.exceptions import TimeoutExceededError
2+
3+
4+
class TestTimeoutExceededError:
5+
def test_default_message(self):
6+
"""Default message should match original behavior."""
7+
err = TimeoutExceededError(resource_type="TrainingJob", status="InProgress")
8+
assert str(err) == (
9+
"Timeout exceeded while waiting for TrainingJob. "
10+
"Final Resource State: InProgress. "
11+
"Increase the timeout and try again."
12+
)
13+
14+
def test_custom_message(self):
15+
"""Custom message should replace the default."""
16+
err = TimeoutExceededError(
17+
resource_type="EvaluationJob",
18+
status="Executing",
19+
message="Your evaluation job is still running. Call .refresh() to check its current status.",
20+
)
21+
assert str(err) == (
22+
"Timeout exceeded while waiting for EvaluationJob. "
23+
"Final Resource State: Executing. "
24+
"Your evaluation job is still running. Call .refresh() to check its current status."
25+
)
26+
27+
def test_default_params(self):
28+
"""No args should use defaults without crashing."""
29+
err = TimeoutExceededError()
30+
assert "(Unkown)" in str(err)
31+
assert "Increase the timeout and try again." in str(err)

‎sagemaker-train/src/sagemaker/train/evaluate/execution.py‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,8 +1154,9 @@ def get_cached_mlflow_url():
11541154
if timeout is not None and time.time() - start_time >= timeout:
11551155
from sagemaker.core.utils.exceptions import TimeoutExceededError
11561156
raise TimeoutExceededError(
1157-
resource_type="PipelineExecution",
1158-
status=current_status
1157+
resource_type="EvaluationJob",
1158+
status=current_status,
1159+
message="Your evaluation job is still running. Call .refresh() to check its current status.",
11591160
)
11601161

11611162
time.sleep(poll)
@@ -1205,8 +1206,9 @@ def get_cached_mlflow_url():
12051206
if timeout is not None and time.time() - start_time >= timeout:
12061207
from sagemaker.core.utils.exceptions import TimeoutExceededError
12071208
raise TimeoutExceededError(
1208-
resource_type="PipelineExecution",
1209-
status=current_status
1209+
resource_type="EvaluationJob",
1210+
status=current_status,
1211+
message="Your evaluation job is still running. Call .refresh() to check its current status.",
12101212
)
12111213

12121214
time.sleep(poll)
@@ -1234,8 +1236,9 @@ def get_cached_mlflow_url():
12341236
if timeout is not None and elapsed >= timeout:
12351237
from sagemaker.core.utils.exceptions import TimeoutExceededError
12361238
raise TimeoutExceededError(
1237-
resource_type="PipelineExecution",
1238-
status=current_status
1239+
resource_type="EvaluationJob",
1240+
status=current_status,
1241+
message="Your evaluation job is still running. Call .refresh() to check its current status.",
12391242
)
12401243

12411244
time.sleep(poll)

‎sagemaker-train/tests/unit/train/evaluate/test_execution.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,10 @@ def test_wait_timeout_exceeded(self, mock_time):
11331133
# Mock time to simulate timeout
11341134
mock_time.side_effect = [0, 10, 20, 30, 40, 50, 60] # Exceeds timeout
11351135

1136-
with pytest.raises(TimeoutExceededError):
1136+
with pytest.raises(TimeoutExceededError, match="EvaluationJob") as exc_info:
11371137
execution.wait(target_status="Succeeded", poll=1, timeout=5)
1138+
assert "still running" in str(exc_info.value)
1139+
assert ".refresh()" in str(exc_info.value)
11381140

11391141
def test_wait_without_pipeline_execution(self):
11401142
"""Test wait when no pipeline execution is set."""

0 commit comments

Comments
 (0)