Skip to content

Commit a432035

Browse files
authored
Better error message for missing secrets (#669)
* Bump pyproject. * Bump pyproject. * Update for PR feedback.
1 parent e821f2b commit a432035

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[tool.poetry]
22
name = "truss"
3-
version = "0.7.7rc2"
3+
version = "0.7.7rc4"
44
description = "A seamless bridge from model development to model delivery"
55
license = "MIT"
66
readme = "README.md"

truss/templates/shared/secrets_resolver.py

+16-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import os
22
from collections.abc import Mapping
33
from pathlib import Path
4-
from typing import Dict
4+
from typing import Dict, Optional
5+
6+
SECRETS_DOC_LINK = "https://truss.baseten.co/docs/using-secrets"
57

68

79
class SecretNotFound(Exception):
@@ -17,7 +19,7 @@ def get_secrets(config: Dict):
1719
return Secrets(config.get("secrets", {}))
1820

1921
@staticmethod
20-
def _resolve_secret(secret_name: str, default_value: str):
22+
def _resolve_secret(secret_name: str, default_value: Optional[str]):
2123
secret_value = default_value
2224
secret_env_var_name = SecretsResolver.SECRET_ENV_VAR_PREFIX + secret_name
2325
if secret_env_var_name in os.environ:
@@ -39,15 +41,11 @@ def __init__(self, base_secrets: Dict[str, str]):
3941

4042
def __getitem__(self, key: str) -> str:
4143
if key not in self._base_secrets:
42-
# Note this is the case where the secrets are not specified in
43-
# config.yaml
44-
raise SecretNotFound(f"Secret '{key}' not specified in the config.")
44+
raise SecretNotFound(_secret_missing_error_message(key))
4545

4646
found_secret = SecretsResolver._resolve_secret(key, self._base_secrets[key])
4747
if not found_secret:
48-
raise SecretNotFound(
49-
f"Secret '{key}' not found. Please check available secrets."
50-
)
48+
raise SecretNotFound(_secret_missing_error_message(key))
5149

5250
return found_secret
5351

@@ -58,3 +56,13 @@ def __iter__(self):
5856

5957
def __len__(self):
6058
return len(self._base_secrets)
59+
60+
61+
def _secret_missing_error_message(key: str) -> str:
62+
return f"""
63+
Secret '{key}' not found. Please ensure that:
64+
* Secret '{key}' is defined in the 'secrets' section of the Truss config file
65+
* The model was pushed with the --trusted flag
66+
* Secret '{key}' is defined in the secret manager
67+
Read more about secrets here: {SECRETS_DOC_LINK}.
68+
"""

truss/tests/test_model_inference.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,9 @@ def predict(self, request):
336336
"""
337337

338338
config_with_no_secret = "model_name: secrets-truss"
339+
missing_secret_error_message = """Secret 'secret' not found. Please ensure that:
340+
* Secret 'secret' is defined in the 'secrets' section of the Truss config file
341+
* The model was pushed with the --trusted flag"""
339342

340343
with ensure_kill_all(), tempfile.TemporaryDirectory(dir=".") as tmp_work_dir:
341344
truss_dir = Path(tmp_work_dir, "truss")
@@ -371,7 +374,7 @@ def predict(self, request):
371374

372375
assert "error" in response.json()
373376

374-
assert_logs_contain_error(container.logs(), "not specified in the config")
377+
assert_logs_contain_error(container.logs(), missing_secret_error_message)
375378
assert "Internal Server Error" in response.json()["error"]
376379

377380
with ensure_kill_all(), tempfile.TemporaryDirectory(dir=".") as tmp_work_dir:
@@ -390,9 +393,7 @@ def predict(self, request):
390393
response = requests.post(full_url, json={})
391394
assert response.status_code == 500
392395

393-
assert_logs_contain_error(
394-
container.logs(), "'secret' not found. Please check available secrets."
395-
)
396+
assert_logs_contain_error(container.logs(), missing_secret_error_message)
396397
assert "Internal Server Error" in response.json()["error"]
397398

398399

0 commit comments

Comments
 (0)