> On Nov 15, 2017, at 08:05, Tres Seaver <[email protected]> wrote: > > On 11/14/2017 09:25 AM, [email protected] > 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/cc6a6ed60260b0391aa38f372b26311d58edf0d6/pyramid/httpexceptions.py#L249 <https://github.com/Pylons/pyramid/blob/cc6a6ed60260b0391aa38f372b26311d58edf0d6/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/cc6a6ed60260b0391aa38f372b26311d58edf0d6/pyramid/httpexceptions.py#L248-L316 > > > Tres. > -- > =================================================================== > Tres Seaver +1 540-429-0999 [email protected] > 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 [email protected]. > To post to this group, send email to [email protected]. > 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 [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/pylons-discuss/9D026DB3-6B57-4EC3-B09E-8A19FA7E7E10%400x58.com. For more options, visit https://groups.google.com/d/optout.
