Skip to content

Commit ed65cfa

Browse files
committed
fix: better errors
1 parent 2e8fdbb commit ed65cfa

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

openedx/core/djangoapps/util/management/commands/dump_settings.py

+16-14
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
import re
77
import sys
88
from datetime import timedelta
9-
from importlib.resources import files
109
from path import Path
1110

1211
from django.conf import settings
13-
from django.core.management.base import BaseCommand, CommandError
14-
from django.test import TestCase
12+
from django.core.management.base import BaseCommand
1513

1614

1715
SETTING_NAME_REGEX = re.compile(r'^[A-Z][A-Z0-9_]*$')
@@ -48,14 +46,14 @@ def handle(self, *args, **kwargs):
4846
Handle the command.
4947
"""
5048
settings_json = {
51-
name: _to_json_friendly_repr(getattr(settings, name))
49+
name: _to_json_friendly_repr(getattr(settings, name), f"settings.{name}")
5250
for name in dir(settings)
5351
if SETTING_NAME_REGEX.match(name)
5452
}
5553
print(json.dumps(settings_json, indent=4))
5654

5755

58-
def _to_json_friendly_repr(value: object) -> object:
56+
def _to_json_friendly_repr(value: object, debug_key: str) -> object:
5957
"""
6058
Turn the value into something that we can print to a JSON file (that is: str, bool, None, int, float, list, dict).
6159
@@ -64,18 +62,20 @@ def _to_json_friendly_repr(value: object) -> object:
6462
if isinstance(value, (type(None), bool, int, float, str)):
6563
# All these types can be printed directly
6664
return value
67-
if isinstance(value, (list, tuple)):
68-
# Print both lists and tuples as JSON arrays
69-
return [_to_json_friendly_repr(element) for element in value]
70-
if isinstance(value, set):
71-
# Print sets by sorting them (so that order doesn't matter) and printing the result as a JSON array
72-
return [sorted(_to_json_friendly_repr(element) for element in value)]
65+
if isinstance(value, (list, tuple, set)):
66+
if isinstance(value, set):
67+
# Print sets by sorting them (so that order doesn't matter) into a JSON array.
68+
elements = sorted(value)
69+
else:
70+
# Print both lists and tuples as JSON arrays.
71+
elements = value
72+
return [_to_json_friendly_repr(element, f"{debug_key}[{ix}]") for ix, element in enumerate(elements)]
7373
if isinstance(value, dict):
7474
# Print dicts as JSON objects
7575
for subkey in value.keys():
7676
if not isinstance(subkey, (str, int)):
7777
raise ValueError(f"Unexpected dict key {subkey} of type {type(subkey)}")
78-
return {subkey: _to_json_friendly_repr(subval) for subkey, subval in value.items()}
78+
return {subkey: _to_json_friendly_repr(subval, f"{debug_key}[{subkey!r}]") for subkey, subval in value.items()}
7979
if isinstance(value, Path):
8080
# Print path objects as the string `Path('path/to/something')`.
8181
return repr(value)
@@ -87,7 +87,7 @@ def _to_json_friendly_repr(value: object) -> object:
8787
if len(proxy_args) == 1:
8888
if isinstance(proxy_args[0], str):
8989
return proxy_args[0]
90-
raise ValueError(f"Not sure how to dump value {value!r} with proxy args {proxy_args!r}")
90+
raise ValueError(f"Not sure how to dump {debug_key} with value {value!r} with proxy args {proxy_args!r}")
9191
if value is sys.stderr:
9292
# Print the stderr object as simply "sys.stderr"
9393
return "sys.stderr"
@@ -97,7 +97,9 @@ def _to_json_friendly_repr(value: object) -> object:
9797
qualname = value.__qualname__
9898
except AttributeError:
9999
# If that doesn't work, then give up--we don't know how to print this value.
100-
raise ValueError(f"Not sure how to dump value {value!r} of type {type(value)}")
100+
raise ValueError( # pylint: disable=raise-missing-from
101+
f"Not sure how to dump {debug_key} with value {value!r} of type {type(value)}"
102+
)
101103
if qualname == "<lambda>":
102104
# Handle lambdas by printing the source lines
103105
return inspect.getsource(value).strip()

0 commit comments

Comments
 (0)