Skip to content

Commit

Permalink
Make OCI config error messages more verbose (#1309)
Browse files Browse the repository at this point in the history
  • Loading branch information
jvstme authored Jun 5, 2024
1 parent c30d012 commit 5109ad4
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 17 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from abc import ABC, abstractmethod
from typing import List, Optional
from typing import Any, List, Optional

from dstack._internal.core.backends.base import Backend
from dstack._internal.core.errors import BackendInvalidCredentialsError
Expand Down Expand Up @@ -59,5 +59,10 @@ def get_backend(self, model: BackendModel) -> Backend:
pass


def raise_invalid_credentials_error(fields: Optional[List[List[str]]] = None):
raise BackendInvalidCredentialsError(fields=fields)
def raise_invalid_credentials_error(
fields: Optional[List[List[str]]] = None, details: Optional[Any] = None
):
msg = BackendInvalidCredentialsError.msg
if details:
msg += f": {details}"
raise BackendInvalidCredentialsError(fields=fields, msg=msg)
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,15 @@ def get_config_values(self, config: OCIConfigInfoWithCredsPartial) -> OCIConfigV
is_core_model_instance(config.creds, OCIDefaultCreds)
and not settings.DEFAULT_CREDS_ENABLED
):
raise_invalid_credentials_error(fields=[["creds"]])
raise_invalid_credentials_error(
fields=[["creds"]],
details="Default credentials are forbidden by dstack settings",
)

try:
available_regions = get_subscribed_regions(config.creds).names & SUPPORTED_REGIONS
except any_oci_exception:
raise_invalid_credentials_error(fields=[["creds"]])
except any_oci_exception as e:
raise_invalid_credentials_error(fields=[["creds"]], details=e)

if config.regions:
selected_regions = [r for r in config.regions if r in available_regions]
Expand All @@ -96,8 +99,8 @@ def create_backend(
) -> BackendModel:
try:
subscribed_regions = get_subscribed_regions(config.creds)
except any_oci_exception:
raise_invalid_credentials_error(fields=[["creds"]])
except any_oci_exception as e:
raise_invalid_credentials_error(fields=[["creds"]], details=e)

if config.regions is None:
config.regions = _filter_supported_regions(subscribed_regions.names)
Expand Down
12 changes: 3 additions & 9 deletions src/tests/_internal/server/routers/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -683,15 +683,9 @@ async def test_returns_invalid_credentials(self, test_db, session: AsyncSession)
)
default_creds_available_mock.assert_called()
assert response.status_code == 400
assert response.json() == {
"detail": [
{
"code": "invalid_credentials",
"msg": "Invalid credentials",
"fields": ["creds"],
},
]
}
error = response.json()["detail"][0]
assert error["code"] == "invalid_credentials"
assert error["msg"].startswith("Invalid credentials")

@pytest.mark.asyncio
async def test_returns_config_on_valid_creds(self, test_db, session: AsyncSession):
Expand Down

0 comments on commit 5109ad4

Please sign in to comment.