predicate mismatch error message #502

Closed
adroullier opened this Issue Mar 19, 2012 · 7 comments

3 participants

@adroullier

The predicate mismatch error message is not really helpful to find the actual problem.
If you're using view classes you only get the class name like

The resource could not be found.
predicate mismatch for view ViewClass 

A possible and quite easy improvement is to add the predicate function that failed:
In pyramid.config.views

    def predicated_view(self, view):
        predicates = self.kw.get('predicates', ())
        if not predicates:
            return view
        def predicate_wrapper(context, request):
            # original
            #if all((predicate(context, request) for predicate in predicates)):
            #    return view(context, request)
            #view_name = getattr(view, '__name__', view)
            #raise PredicateMismatch(
            #    'predicate mismatch for view %s' % view_name)

            # split up all() and add the predicate to the exception
            for predicate in predicates:
                if not predicate(context, request):
                    view_name = getattr(view, '__name__', view)
                    raise PredicateMismatch(
                        'predicate mismatch for view %s (%s)' % (view_name, str(predicate)))
            return view(context, request)        

        def checker(context, request):
            return all((predicate(context, request) for predicate in
                        predicates))
        predicate_wrapper.__predicated__ = checker
        predicate_wrapper.__predicates__ = predicates
        return predicate_wrapper
@mgedmin

I wonder if using predicate.__text__ instead of str(predicate) would not be a better idea here.

@adroullier

Where is text defined? Do the pyramid predicates already have extended information?
If you define your own function and register it for a view it does not have a text attribute. However a better description than str(predicate) would make sense.

@mgedmin

Where is __text__ defined?

In make_predicates, which is invoked by add_view. User-defined custom_predicates can also define their own __text__ and if they don't, they get one assigned by make_predicates.

Now, I'm not actually a Pyramid developer, I just like reading source code. I may have misunderstood something, so take my advice with a grain of salt. But if I'm right, this will result in clearer error messages.

@adroullier

Yes, you're right. And I just tried it (finally).
predicate.text already contains formatted context info.

Exceptions would look similar to this one and contain all the info you need:

predicate mismatch for view ViewClass (containment = <class 'models.model1'>)
@mcdonc
Pylons Project member

If a patch was worked up for this I'd apply it.

@mcdonc
Pylons Project member

Thanks for the pull reuqest. Do you think you could update the tests to reflect the change?

@mcdonc mcdonc added a commit that referenced this issue Jul 29, 2012
@mcdonc mcdonc - When there is a predicate mismatch exception (seen when no view mat…
…ches for

  a given request due to predicates not working), the exception now contains
  a textual description of the predicate which didn't match.

- Fixes issue #502 and issue #519.
61a57ea
@mcdonc
Pylons Project member

The above commit closes this issue. I wrote the tests.

@mcdonc mcdonc closed this Jul 29, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment