diff options
author | Jason Madden <jamadden@gmail.com> | 2018-09-28 07:34:03 -0500 |
---|---|---|
committer | Jason Madden <jamadden@gmail.com> | 2018-09-28 07:34:03 -0500 |
commit | 6eb8ddc6d99537757031676550a093c9552f867c (patch) | |
tree | 97666c7eff28ae3d7ba53e73f6fec1f8744631f1 | |
parent | bf468b939411a785dbab5344cfc471369975edd8 (diff) | |
download | zope-configuration-6eb8ddc6d99537757031676550a093c9552f867c.tar.gz |
Simplify detail formatting and 100% coverage.
-rw-r--r-- | src/zope/configuration/config.py | 24 | ||||
-rw-r--r-- | src/zope/configuration/exceptions.py | 20 | ||||
-rw-r--r-- | src/zope/configuration/xmlconfig.py | 4 |
3 files changed, 35 insertions, 13 deletions
diff --git a/src/zope/configuration/config.py b/src/zope/configuration/config.py index 643ff2c..6ce6d06 100644 --- a/src/zope/configuration/config.py +++ b/src/zope/configuration/config.py @@ -727,7 +727,7 @@ class ConfigurationMachine(ConfigurationAdapterRegistry, ConfigurationContext): [('f', (1,), {}), ('f', (2,), {})] If the action raises an error, we convert it to a - ConfigurationExecutionError. + `ConfigurationExecutionError`. >>> from zope.configuration.config import ConfigurationExecutionError >>> output = [] @@ -750,6 +750,24 @@ class ConfigurationMachine(ConfigurationAdapterRegistry, ConfigurationContext): >>> output [('f', (1,), {}), ('f', (2,), {})] + + If the exception was already a `ConfigurationError`, it is raised + as-is with the action's ``info`` added. + + >>> def bad(): + ... raise ConfigurationError("I'm bad") + >>> context.actions = [ + ... (1, f, (1,)), + ... (1, f, (11,), {}, ('x', )), + ... (2, f, (2,)), + ... (3, bad, (), {}, (), 'oops') + ... ] + >>> context.execute_actions() + Traceback (most recent call last): + ... + zope.configuration.exceptions.ConfigurationError: I'm bad + oops + """ pass_through_exceptions = self.pass_through_exceptions if testing: @@ -765,7 +783,7 @@ class ConfigurationMachine(ConfigurationAdapterRegistry, ConfigurationContext): try: callable(*args, **kw) except ConfigurationError as ex: - ex.append_details(info) + ex.add_details(info) raise except pass_through_exceptions: raise @@ -1660,7 +1678,7 @@ def toargs(context, schema, data): try: args[str(name)] = field.fromUnicode(s) except ValidationError as v: - reraise(ConfigurationError("Invalid value for %r: %r" % (n, v)).append_details(v), + reraise(ConfigurationError("Invalid value for %r: %r" % (n, v)).add_details(v), None, sys.exc_info()[2]) elif field.required: # if the default is valid, we can use that: diff --git a/src/zope/configuration/exceptions.py b/src/zope/configuration/exceptions.py index 1262035..abac17a 100644 --- a/src/zope/configuration/exceptions.py +++ b/src/zope/configuration/exceptions.py @@ -26,9 +26,11 @@ class ConfigurationError(Exception): # A list of strings or things that can be converted to strings, # added by append_details as we walk back up the call/include stack. + # We will print them in reverse order so that the most recent detail is + # last. _details = () - def append_details(self, info): + def add_details(self, info): if isinstance(info, BaseException): info = traceback.format_exception_only(type(info), info) # Trim trailing newline @@ -38,17 +40,19 @@ class ConfigurationError(Exception): self._details += (info,) return self + def _with_details(self, opening, detail_formatter): + lines = [' ' + detail_formatter(detail) for detail in self._details] + lines.append(opening) + lines.reverse() + return '\n'.join(lines) + def __str__(self): s = super(ConfigurationError, self).__str__() - for i in self._details: - s += '\n ' + str(i) - return s + return self._with_details(s, str) def __repr__(self): s = super(ConfigurationError, self).__repr__() - for i in self._details: - s += '\n ' + repr(i) - return s + return self._with_details(s, repr) class ConfigurationWrapperError(ConfigurationError): @@ -57,7 +61,7 @@ class ConfigurationWrapperError(ConfigurationError): def __init__(self, info, exception): super(ConfigurationWrapperError, self).__init__(repr(info) if self.USE_INFO_REPR else info) - self.append_details(exception) + self.add_details(exception) # This stuff is undocumented and not used but we store # for backwards compatibility diff --git a/src/zope/configuration/xmlconfig.py b/src/zope/configuration/xmlconfig.py index b12b091..4fc0d0d 100644 --- a/src/zope/configuration/xmlconfig.py +++ b/src/zope/configuration/xmlconfig.py @@ -228,7 +228,7 @@ class ConfigurationHandler(ContentHandler): if self.testing: raise if isinstance(ex, ConfigurationError): - ex.append_details(repr(info)) + ex.add_details(repr(info)) raise exc = ZopeXMLConfigurationError(info, ex) @@ -406,7 +406,7 @@ def processxmlfile(file, context, testing=False): try: parser.parse(src) except SAXParseException: - reraise(ZopeSAXParseException(src, sys.exc_info()[1]), + reraise(ZopeSAXParseException(file, sys.exc_info()[1]), None, sys.exc_info()[2]) |