zopefoundation / zope.configuration

Extensible system for supporting various kinds of configurations
https://zopeconfiguration.readthedocs.io
Other
1 stars 6 forks source link

No Error Msg re Invalid ZCML Attribute #4

Closed tseaver closed 5 years ago

tseaver commented 9 years ago

Via https://bugs.launchpad.net/zope.configuration/+bug/98257:

When an unsupport attribute is used with a ZCML tag, you get a traceback but no indication of what the problem is. In the example below, removing the layer="webaccountant" attribute fixes the problem, but to find that out you have to iteratively remove each attribute one-by-one.

BTW, the layer attribute is supported in the books and, in the code there seems to be a movement away from string layer names to interfaces. However, the deprecation warning only keys off the layer name "default", and is bypassed if you specify a customer layer name. I think that then confuses the code.

$ bin/runzope
Traceback (most recent call last):
  File "bin/runzope", line 48, in ?
    run()
  File "bin/runzope", line 44, in run
    main(["-C", CONFIG_FILE] + sys.argv[1:])
  File "/usr/share/zope-3.1.0-py2.4/lib/python/zope/app/server/main.py", line 58, in main
    setup(load_options(args))
  File "/usr/share/zope-3.1.0-py2.4/lib/python/zope/app/server/main.py", line 169, in setup
    zope.app.appsetup.config(options.site_definition)
  File "/usr/share/zope-3.1.0-py2.4/lib/python/zope/app/appsetup/appsetup.py", line 52, in config
    context = xmlconfig.file(file, execute=execute)
  File "/usr/share/zope-3.1.0-py2.4/lib/python/zope/configuration/xmlconfig.py", line 557, in file
    context.execute_actions()
  File "/usr/share/zope-3.1.0-py2.4/lib/python/zope/configuration/config.py", line 622, in execute_actions
    callable(*args, **kw)
  File "/usr/share/zope-3.1.0-py2.4/lib/python/zope/app/component/metaconfigure.py", line 42, in handler
    method(*args, **kwargs)
  File "/usr/share/zope-3.1.0-py2.4/lib/python/zope/component/site.py", line 171, in provideAdapter
    raise TypeError(iface, IInterface)
zope.configuration.config.ConfigurationExecutionError: exceptions.TypeError: (<module 'webaccountant' from '/var/hostings/98tau0417/taupro.com/zope3/lib/python/webaccountant/__init__.pyc'>, <InterfaceClass zope.interface.interfaces.IInterface>)
  in:
  File "/var/hostings/98tau0417/taupro.com/zope3/lib/python/webaccountant/ventures/browser/configure.zcml", line 166.6-184.6
        <browser:view
            name="+"
            for="webaccountant.ventures.interfaces.IVentureSet"
            class="zope.app.container.browser.adding.Adding"
            layer="webaccountant"
            menu="zmi_actions" title="Add"
            permission="zope.ManageContent"
            >
            <browser:page
                name="index.html"
                attribute="index"
                />
            <browser:page
                name="action.html"
                attribute="action"
                />
        </browser:view>

@philikon replied:

The 'layer' attribute to <browser:view /> is actually perfectly legal, so your assumption that the error message doesn't display the correct thing is not quite correct.

If you look at the traceback carefully, you will see that the ZCML machinery complains that the "webaccountant" module doesn't provide IInterface. This indicates that ZCML is confusing the "webaccountant" module for your layer. If I'm correct, there's only one way that can happen: you forgot to define the layer via <browser:layer />.

zope.app.component.fields.LayerField should be made to check if the layer object it retrieves by resolving a dottedname actually provides IIinterface. If that isn't the case, it should present nice error message, for example: "The XXX object of type XXX is not an interface."

jamadden commented 5 years ago

I don't think this is a problem that can be solved inzope.configuration. It would be up to specific directives and fields to produce reasonable errors.

For attributes that aren't supported at all, toargs already produces nice errors, for all levels of the directive tree:

zope.configuration.xmlconfig.ZopeXMLConfigurationError: File "<string>", line 8.4-11.37
    ConfigurationError: ('Unrecognized parameters:', 'biz')

The adapter field specifies interfaces now and raises an error if misconfigured:

zope.configuration.xmlconfig.ZopeXMLConfigurationError: File "<string>", line 6.2-6.79
    ConfigurationError: ('Invalid value for', 'provides', "(<class '__main__.Foo'>, <InterfaceClass zope.interface.interfaces.IInterface>, '')")

(I will say that the original NotAnInterface error is getting lost, which is a shame. But on Python 3, the chained tracebacks make this easier to deal with.)

If a particular directive is calling zope.component.provideAdapter directly (which it shouldn't be anymore), it would be that directive's responsibility to provide a decent error or validation.