Skip to content

Commit

Permalink
Notification: fix choices rendering for INVALID_CHOICE (#11190)
Browse files Browse the repository at this point in the history
* Notification: fix `choices` rendering for `INVALID_CHOICE`

While taking a look at #11189 I found that the rendering was broken.
I found the `with` + `yield` pattern pretty complex (I have a note about this in
the code) and it seems that complexity is confusing enough to make this hard to
dealt with.

Anyways, I fixed the problem for now and I added a test case. We can come back
to refactoring this in the future with more time.

* Update readthedocs/config/config.py

Co-authored-by: Santos Gallegos <stsewd@proton.me>

---------

Co-authored-by: Santos Gallegos <stsewd@proton.me>
  • Loading branch information
humitos and stsewd committed Mar 6, 2024
1 parent e441115 commit 7880e85
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
15 changes: 11 additions & 4 deletions readthedocs/config/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,20 @@ def catch_validation_error(self, key):
try:
yield
except ConfigValidationError as error:
raise ConfigError(
message_id=error.message_id,
format_values={
# Expand the format values defined when the exception is risen
# with extra ones we have here
format_values = getattr(error, "format_values", {})
format_values.update(
{
"key": key,
"value": error.format_values.get("value"),
"source_file": os.path.relpath(self.source_file, self.base_path),
},
}
)

raise ConfigError(
message_id=error.message_id,
format_values=format_values,
) from error

def pop(self, name, container, default, raise_ex):
Expand Down
3 changes: 3 additions & 0 deletions readthedocs/config/tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ def test_new_build_config_invalid_tools_version(self):
build.validate()
assert excinfo.value.message_id == ConfigValidationError.INVALID_CHOICE
assert excinfo.value.format_values.get("key") == "build.tools.python"
assert excinfo.value.format_values.get("choices") == ", ".join(
settings.RTD_DOCKER_BUILD_SETTINGS["tools"]["python"].keys()
)

def test_new_build_config(self):
build = get_build_config(
Expand Down

0 comments on commit 7880e85

Please sign in to comment.