> The issue I think here is that he is “raising” the HTTPNotFound() instead
of just returning it. At some point this HTTPNotFound that you’ve created
needs to be sent back as a response.

Ok first, Pyramid already has a HTTPException handler which will catch and
return the exception as a response so this @exception_view_config is not
necessary.

Second, everything actually works completely fine and whatever Sascha is
doing in his app is breaking things outside of his pasted example. You can
see below this example works as intended:

import json
from pyramid.httpexceptions import HTTPNotFound
from pyramid.view import view_config

@view_config(route_name='problem')
def problem_view(request):
    exc = HTTPNotFound()
    exc.text = json.dumps({'message': 'foobar'})
    exc.content_type = 'application/problem+json'
    raise exc

if __name__ == '__main__':
    from pyramid.config import Configurator
    from waitress import serve

    config = Configurator()
    config.add_route('problem', '/problem')
    config.scan(__name__)
    app = config.make_wsgi_app()

    serve(app)

~❯ curl -D- http://localhost:8080/problem
HTTP/1.1 404 Not Found
Content-Length: 21
Content-Type: application/problem+json
Date: Wed, 15 Nov 2017 19:51:20 GMT
Server: waitress

{"message": "foobar"}%

- Michael


On Wed, Nov 15, 2017 at 12:43 PM, Bert JW Regeer <xiste...@0x58.com> wrote:

>
>
> On Nov 15, 2017, at 08:05, Tres Seaver <tsea...@palladion.com> wrote:
>
> On 11/14/2017 09:25 AM, webmas...@johlia.de
> wrote:
>
> I have a pyramid application. How can I change the content_type to
> problem+json (as defined in: https://tools.ietf.org/html/rfc7807#page-9)
>
>
> Interesting -- thanks for pointing that out!  I'd never come across that
> RFC before.
>
> My code looks like this:
> from pyramid.httpexceptions import HTTPNotFound
> def error_handler(self, message):
>        response = HTTPNotFound()
>        response.body = dumps({'message': message})
>        response.content_type = 'application/json'
>        raise response
>
> Changing the content_type to application/problem+json doesn't work.
>
>
> 'HTTPExecption.prepare'[1] is overriding that content type based on the
> 'Accept:' header detection.  Making that method aware of possible extension
> types seems like a reasonable choice:  the fastest way to get that in place
> would be a pull request with tests.
>
>
> https://github.com/Pylons/pyramid/blob/cc6a6ed60260b0391aa38f372b2631
> 1d58edf0d6/pyramid/httpexceptions.py#L249
>
> This should be checking if the response already has a body, in which case
> prepare won’t do any work…
>
> Since the response.body is already set, that check should just
> short-circuit prepare and not do anything.
>
> The issue I think here is that he is “raising” the HTTPNotFound() instead
> of just returning it. At some point this HTTPNotFound that you’ve created
> needs to be sent back as a response.
>
> @exception_view_config(HTTPNotFound)
> def ret_error(exc, request):
>     return exc
>
> For example will return the HTTPNotFound that was raised elsewhere in your
> code. Body/content_type will stay in tact.
>
> If you don’t register an exception view for HTTPNotFound your raise will
> get caught by the exception view tween, and it will try to find a view for
> it, and upon failure it will raise a HTTPNotFound.
>
>
> In the meanwhile, ISTM that you'll either need to monkey-patch
> 'HTTPResponse.prepare' or else stop using exception types which derive from
> 'HTTPException'.  E.g.:
>
>    from pyramid.response import Response
>
>    class ProblemResponse(Response, Exception):
>        pass
>
>    def error_handler(self, message):
>        raise ProblemResponse(
>            body=dumps({'message': message},
>            status='404 Not Found',
>            content_type = 'application/problem+json',
>        )
>
>
> [1]https://github.com/Pylons/pyramid/blob/cc6a6ed60260b0391aa38f372b2631
> 1d58edf0d6/pyramid/httpexceptions.py#L248-L316
>
>
> Tres.
> --
> ===================================================================
> Tres Seaver          +1 540-429-0999 <(540)%20429-0999>
> tsea...@palladion.com
> Palladion Software   "Excellence by Design"    http://palladion.com
>
> --
> 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-discuss+unsubscr...@googlegroups.com.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/pylons-discuss/ouhl59%242cf%241%40blaine.gmane.org.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> 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-discuss+unsubscr...@googlegroups.com.
> To post to this group, send email to pylons-discuss@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/
> msgid/pylons-discuss/9D026DB3-6B57-4EC3-B09E-8A19FA7E7E10%400x58.com
> <https://groups.google.com/d/msgid/pylons-discuss/9D026DB3-6B57-4EC3-B09E-8A19FA7E7E10%400x58.com?utm_medium=email&utm_source=footer>
> .
>
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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-discuss+unsubscr...@googlegroups.com.
To post to this group, send email to pylons-discuss@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/pylons-discuss/CAKdhhwHP1rYP8a5J%2BB2_WtMo_z5SLXexP2J2-UL_3sL3KbP4YA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to