Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Custom HTTP error on Cornice view predicate

15 views
Skip to first unread message

Thierry Florac

unread,
Feb 13, 2025, 5:22:46 PMFeb 13
to pylons-...@googlegroups.com
Hi,
I'm building a REST API using Pyramid and Cornice for which I defined custom view predicates. By default, an HTTP NotFound error is raised when a predicate is not verified. Is there a way to raise a custom error, like an HTTP Unauthorized or HTTP Forbidden?

Best regards,
Thierry

Theron Luhn

unread,
Feb 13, 2025, 5:37:03 PMFeb 13
to pylons-...@googlegroups.com
You might be looking for something like view derivers https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hooks.html#custom-view-derivers

That will allow you to accept additional parameters in `add_view`/`@view_config` and wrap the view in customer behavior, such as raising an HTTPForbidden if a certain condition isn’t met.

--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pylons-discus...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/pylons-discuss/CAPX_VWAa46kYQZeh3-vsS5xB2XCrdL_P73xOew8xf-AgoefrNw%40mail.gmail.com.

Thierry Florac

unread,
Feb 14, 2025, 6:26:24 AMFeb 14
to pylons-...@googlegroups.com
I tried to use permission predicate/deriver, but an HTTP NotFound exception is raised if the permission is not granted to the current request!
I think that an HTTP Forbidden exception would be more appropriate to handle automatic redirection to a login form from the client application...

Best regards,
Thierry

Theron Luhn

unread,
Feb 14, 2025, 12:21:47 PMFeb 14
to pylons-...@googlegroups.com
You say “predicate/deriver” but those are two different things.

Predicates will influence what view matches the request but do not modify the behavior of the view callable.  If no view is found a 404 is raised.

View derivers do the opposite:  They modify the behavior of the view callable but do not influence choosing the view.

The default Pyramid permission view deriver will raise HTTP Forbidden if the permission check fails.  I’m not familiar with Cornice so I can’t speak to anything there.

— Theron



Reply all
Reply to author
Forward
0 new messages