Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

vdk-core: domain specific properties/secrets exceptions #2770

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
from vdk.api.job_input import IProperties
from vdk.api.plugin.plugin_input import IPropertiesServiceClient

from ...core.errors import report_and_throw
from ...core.errors import UserCodeError
from ...core.errors import ResolvableBy
from .base_properties_impl import check_valid_property
from .exception import WritePreProcessPropertiesException

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -82,15 +82,11 @@ def set_all_properties(self, properties: Dict[str, PropertyValue]) -> None:
self._job_name, self._team_name, properties
)
except Exception as e:
report_and_throw(
UserCodeError(
f"A write pre-processor of properties client {client} had failed.",
f"User Error occurred. Exception was: {e}",
"PROPERTIES_WRITE_PREPROCESS_SEQUENCE was interrupted, and "
"properties won't be written by the PROPERTIES_DEFAULT_TYPE client.",
"Handle the exception raised.",
)
)
raise WritePreProcessPropertiesException(
client=client,
preprocess_sequence=str(self._write_preprocessors),
resolvable_by=ResolvableBy.USER_ERROR,
) from e

for k, v in list(properties.items()):
check_valid_property(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
from typing import Optional

from vdk.internal.core.errors import BaseVdkError
from vdk.internal.core.errors import ResolvableBy


class PropertiesException(BaseVdkError):
"""
Base Exception for all custom exceptions related to the properties.
"""

def __init__(
self,
message: Optional[str] = None,
resolvable_by: Optional[ResolvableBy] = None,
):
super().__init__(None, resolvable_by, message)
antoniivanov marked this conversation as resolved.
Show resolved Hide resolved


class WritePreProcessPropertiesException(PropertiesException):
def __init__(
self,
message: Optional[str] = None,
client="unknown",
preprocess_sequence: str = "",
resolvable_by: Optional[ResolvableBy] = None,
):
if not message:
message = (
f"Write pre-processor for client {client} failed. "
f"preprocess sequence is {preprocess_sequence}. "
f"No properties are updated."
)
super().__init__(message, resolvable_by)
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@
from vdk.api.plugin.plugin_input import ISecretsServiceClient

from ...core.errors import report_and_throw
from ...core.errors import ResolvableBy
from ...core.errors import UserCodeError
from .base_secrets_impl import check_valid_secret
from .exception import WritePreProcessSecretsException

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -80,15 +82,11 @@ def set_all_secrets(self, secrets: Dict[str, SecretValue]) -> None:
self._job_name, self._team_name, secrets
)
except Exception as e:
report_and_throw(
UserCodeError(
f"A write pre-processor of secrets client {client} had failed.",
f"User Error occurred. Exception was: {e}",
"SECRETS_WRITE_PREPROCESS_SEQUENCE was interrupted, and "
"secrets won't be written by the SECRETS_DEFAULT_TYPE client.",
"Handle the exception raised.",
)
)
raise WritePreProcessSecretsException(
client=client,
preprocess_sequence=str(self._write_preprocessors),
resolvable_by=ResolvableBy.USER_ERROR,
) from e

for k, v in list(secrets.items()):
check_valid_secret(k, v, DataJobsServiceSecrets.__VALID_TYPES) # throws
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Copyright 2021-2023 VMware, Inc.
# SPDX-License-Identifier: Apache-2.0
from typing import Optional

from vdk.internal.core.errors import BaseVdkError
from vdk.internal.core.errors import ResolvableBy


class SecretsException(BaseVdkError):
"""
Base Exception for all custom exceptions related to the properties.
"""

def __init__(
self,
message: Optional[str] = None,
resolvable_by: Optional[ResolvableBy] = None,
):
super().__init__(None, resolvable_by, message)
antoniivanov marked this conversation as resolved.
Show resolved Hide resolved


class WritePreProcessSecretsException(SecretsException):
def __init__(
self,
message: Optional[str] = None,
client="unknown",
preprocess_sequence: str = "",
resolvable_by: Optional[ResolvableBy] = None,
):
if not message:
message = (
f"Write pre-processor for client {client} failed. "
f"preprocess sequence is {preprocess_sequence}. "
f"No properties are updated."
)
super().__init__(message, resolvable_by)
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import pytest
from vdk.api.plugin.plugin_input import IPropertiesServiceClient
from vdk.internal.builtin_plugins.config.job_config import JobConfigKeys
from vdk.internal.builtin_plugins.job_properties.exception import (
WritePreProcessPropertiesException,
)
from vdk.internal.builtin_plugins.job_properties.properties_router import (
PropertiesRouter,
)
from vdk.internal.core import errors
from vdk.internal.core.config import Configuration
from vdk.internal.core.errors import ResolvableBy
from vdk.internal.core.errors import UserCodeError
from vdk.internal.core.errors import VdkConfigurationError

Expand Down Expand Up @@ -157,8 +162,12 @@ def test_preprocessing_sequence_error():
router.set_properties_factory_method("foo", lambda: foo_mock_client)
router.set_properties_factory_method("bar", lambda: bar_mock_client)

with pytest.raises(UserCodeError):
with pytest.raises(WritePreProcessPropertiesException) as exc_info:
router.set_all_properties({"a": "b"})
assert (
errors.get_exception_resolvable_by(exc_info.value)
== ResolvableBy.USER_ERROR
)


def test_preprocessing_sequence_misconfigured():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
import pytest
from vdk.api.plugin.plugin_input import ISecretsServiceClient
from vdk.internal.builtin_plugins.config.job_config import JobConfigKeys
from vdk.internal.builtin_plugins.job_secrets.exception import (
WritePreProcessSecretsException,
)
from vdk.internal.builtin_plugins.job_secrets.secrets_router import (
SecretsRouter,
)
from vdk.internal.core import errors
from vdk.internal.core.config import Configuration
from vdk.internal.core.errors import ResolvableBy
from vdk.internal.core.errors import UserCodeError
from vdk.internal.core.errors import VdkConfigurationError

Expand Down Expand Up @@ -147,8 +152,12 @@ def test_preprocessing_sequence_error():
router.set_secrets_factory_method("foo", lambda: foo_mock_client)
router.set_secrets_factory_method("bar", lambda: bar_mock_client)

with pytest.raises(UserCodeError):
with pytest.raises(WritePreProcessSecretsException) as exc_info:
router.set_all_secrets({"a": "b"})
assert (
errors.get_exception_resolvable_by(exc_info.value)
== ResolvableBy.USER_ERROR
)


def test_preprocessing_sequence_misconfigured():
Expand Down