mirror of
https://github.com/Steffo99/cfig.git
synced 2024-11-24 17:04:19 +00:00
Display a fancy error message if cfig.errors.BatchResolutionFailure is raised
This commit is contained in:
parent
455d41669c
commit
fa2e360767
2 changed files with 35 additions and 1 deletions
|
@ -79,9 +79,41 @@ class InvalidValueError(ConfigurationError):
|
||||||
class BatchResolutionFailure(BaseException):
|
class BatchResolutionFailure(BaseException):
|
||||||
"""
|
"""
|
||||||
A cumulative error which sums the errors occurred while resolving proxied configuration values.
|
A cumulative error which sums the errors occurred while resolving proxied configuration values.
|
||||||
|
|
||||||
|
It inherits from :class:`BaseException` to be distinguishable from regular :class:`Exception`s occouring inside the resolvers.
|
||||||
|
|
||||||
|
It uses some formatting tricks to display the missing keys in the configuration error message:
|
||||||
|
|
||||||
|
.. code-block:: console
|
||||||
|
|
||||||
|
$ python -m cfig.sample.usage
|
||||||
|
Traceback (most recent call last):
|
||||||
|
...
|
||||||
|
File "./cfig/sample/usage.py", line 7, in <module>
|
||||||
|
config.proxies.resolve()
|
||||||
|
File "./cfig/config.py", line 59, in resolve
|
||||||
|
raise errors.BatchResolutionFailure(errors=errors_dict)
|
||||||
|
cfig.errors.BatchResolutionFailure: 4 errors occurred during the resolution of the config:
|
||||||
|
* EXAMPLE_NUMBER → InvalidValueError: Not an int.
|
||||||
|
* TELEGRAM_BOT_TOKEN → MissingValueError: TELEGRAM_BOT_TOKEN
|
||||||
|
* DISCORD_CLIENT_SECRET → MissingValueError: DISCORD_CLIENT_SECRET
|
||||||
|
* DATABASE_URI → MissingValueError: DATABASE_URI
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, errors: dict[str, Exception]):
|
def __init__(self, errors: dict[str, Exception]):
|
||||||
|
message = [f"{len(errors)} errors occurred during the resolution of the config:"]
|
||||||
|
|
||||||
|
key_padding = max(map(lambda k: len(k), errors.keys()))
|
||||||
|
|
||||||
|
for key, val in errors.items():
|
||||||
|
# Weird padding hack, part 2
|
||||||
|
# noinspection PyStringFormat
|
||||||
|
key_text = f"{{key:{key_padding}}}".format(key=key)
|
||||||
|
|
||||||
|
message.append(f"* {key_text} → {val.__class__.__qualname__}: {val}")
|
||||||
|
|
||||||
|
super().__init__("\n".join(message))
|
||||||
|
|
||||||
self.errors: dict[str, Exception] = errors
|
self.errors: dict[str, Exception] = errors
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|
|
@ -2,7 +2,9 @@
|
||||||
This module contains an example of how to use the values defined in a cfig definition module.
|
This module contains an example of how to use the values defined in a cfig definition module.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from .definition import EXAMPLE_STRING, EXAMPLE_NUMBER, DATABASE_ENGINE
|
from .definition import EXAMPLE_STRING, EXAMPLE_NUMBER, DATABASE_ENGINE, config
|
||||||
|
|
||||||
|
config.proxies.resolve()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
Loading…
Reference in a new issue