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

Replace ValueError with RasaException in replace_environment_variables function #8382

Merged
merged 5 commits into from
Apr 7, 2021
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
2 changes: 2 additions & 0 deletions changelog/8382.bugfix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Throw `RasaException` instead of `ValueError` in situations when environment variables
specified in YAML cannot be expanded.
2 changes: 1 addition & 1 deletion rasa/shared/core/domain.py
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,7 @@ def is_domain_file(filename: Text) -> bool:

try:
content = rasa.shared.utils.io.read_yaml_file(filename)
except (ValueError, YamlSyntaxException):
except (RasaException, YamlSyntaxException):
return False

return any(key in content for key in ALL_DOMAIN_KEYS)
Expand Down
11 changes: 5 additions & 6 deletions rasa/shared/utils/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
FileIOException,
FileNotFoundException,
YamlSyntaxException,
RasaException,
)
import rasa.shared.utils.validation

Expand Down Expand Up @@ -314,10 +315,9 @@ def env_var_constructor(loader, node):
w for w in expanded_vars.split() if w.startswith("$") and w in value
]
if not_expanded:
raise ValueError(
"Error when trying to expand the environment variables"
" in '{}'. Please make sure to also set these environment"
" variables: '{}'.".format(value, not_expanded)
raise RasaException(
f"Error when trying to expand the environment variables in '{value}'. "
f"Please make sure to also set these environment variables: '{not_expanded}'."
)
return expanded_vars

Expand All @@ -333,8 +333,7 @@ def read_yaml(content: Text, reader_type: Union[Text, List[Text]] = "safe") -> A

Args:
content: A text containing yaml content.
reader_type: Reader type to use. By default "safe" will be used
replace_env_vars: Specifies if environment variables need to be replaced
reader_type: Reader type to use. By default "safe" will be used.

Raises:
ruamel.yaml.parser.ParserError: If there was an error when parsing the YAML.
Expand Down
8 changes: 3 additions & 5 deletions tests/shared/utils/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
from collections import OrderedDict
from pathlib import Path
from typing import Callable, Text, List, Set, Any, Dict
from _pytest.monkeypatch import MonkeyPatch
from mock import Mock

import pytest

import rasa.shared
from rasa.shared.exceptions import FileIOException, FileNotFoundException
from rasa.shared.exceptions import FileIOException, FileNotFoundException, RasaException
import rasa.shared.utils.io
import rasa.shared.utils.validation
from rasa.shared.constants import NEXT_MAJOR_VERSION_FOR_DEPRECATIONS
Expand Down Expand Up @@ -147,13 +145,13 @@ def test_read_yaml_string_with_env_var_not_exist():
user: ${USER_NAME}
password: ${PASSWORD}
"""
with pytest.raises(ValueError):
with pytest.raises(RasaException):
rasa.shared.utils.io.read_yaml(config_with_env_var_not_exist)


def test_environment_variable_not_existing():
content = "model: \n test: ${variable}"
with pytest.raises(ValueError):
with pytest.raises(RasaException):
rasa.shared.utils.io.read_yaml(content)


Expand Down