Cornices / cornice

Build Web Services with Pyramid.
https://cornice.readthedocs.io
Other
384 stars 149 forks source link

Combining `request.error` with Pyramid exceptions. #526

Open jenstroeger opened 4 years ago

jenstroeger commented 4 years ago

Considering that Pyramid HTTPExceptions inherit from Response and Exception I can raise or return such objects. However, I was wondering how that plays along with Cornice’s request.errors dictionary.

What’s the recommended way of filling that errors dictionary and then raising an HTTP error response—if that’s doable at all?

Something like

request.errors.add("body", "foo", "Invalid foo specified.")
request.errors.status = 404  # Not found
raise HTTPNotFound()

creates a new Response and the errors dictionary isn’t being used, correct?

jenstroeger commented 4 years ago

Addendum Should have tried before I asked. That’s exactly how it works, actually.

@leplatrem, mind if I expand on the documentation a bit? This is quite a handy detail. Or did I miss it somewhere?

leplatrem commented 4 years ago

@jenstroeger Of course! please do!

jenstroeger commented 4 years ago

@leplatrem, done with this PR.