On Thu, Apr 3, 2008 at 5:48 PM, Kumar McMillan <[EMAIL PROTECTED]> wrote:
> Hi, I feel like I'm going around in circles trying to figure out how
>  to serve JSON error documents for my jsonified controllers.  I'm using
>  Pylons 0.9.6.1 and I've been following:
>  http://wiki.pylonshq.com/display/pylonsdocs/Error+Documents
>
>  I put a json template string in
>  <app>.controllers.error.ErrorController but I have two questions [so
>  far]  :
>
>  1. What's a good way to decide whether or not I want to send a JSON
>  response or an HTML response?  I was thinking
> pylons.response.headers['Content-Type'] but it seems that even though
> @jsonify is setting the Content-Type, something is resetting it.

the best idea I could come up with is in a custom version of @jsonify,
where I'm setting environ['<app>.json_response'] = True before calling
the decorated func.

the reason the Content-type header doesn't work is because
paste.HTTPException.httpexceptions.prepare_content() is resetting the
Content-type to text/html.  I mustered up a patch and a test to make
paste JSON aware, but it needs review and I'm a little uncertain if
it's the right place for that kind of logic.  If anyone from paste is
listening (cc'ing paste users, sorry for cross posting) just let me
know and I'll submit the patch to paste for review.

then ... I have a custom error_mapper that adds the 500 error code
even in debug mode so that jsonified requests are *always* json
responses.  My custom error document also looks for
environ['<app>.json_response'] and when True sends the error message
in JSON.

eh, all this reliance on environ doesn't seem right but, hey, it works [for now]

>
>  2. How the heck do I test this?  I really really don't want to create
>  a jsonified controller method in my real app just to raise an error
>  for a test.  I want to create a small controller in my test module and
>  then somehow configure ErrorDocuments with my very own
>  <app>.controllers.error ... but I'm having a hard time finding where
>  that magic occurs.  So far I have a mess of a file, with pieces from
>  Pylon's own unit tests, setting up a test app like this:
>
>  environ = { 'paste.throw_errors': False, 'debug': False }
>  app = ControllerWrap(JsonController)
>  app = ErrorHandler(app, environ, error_template=error_template,
>         debug=False, show_exceptions_in_wsgi_errors=True)
>  app = ErrorDocuments(app, environ, mapper=error_mapper)
>  app = sap = SetupCacheGlobal(app, environ)
>  app = RegistryManager(app)
>  app = TestApp(app)
>
>  ... with JsonController being the one that does a little abort(500,
>  'internal server error').  I suppose I need to connect
>  /error/document/ to my other controller with routes somehow?  Or maybe
>  just unit test the ErrorController?

I couldn't come up with a way to test an app by simply constructing a
pylons controller that raised an error in a @jsonified method (as
explained above).  Instead I tested the error_mapper to make sure it
does the status code switching for jsonified methods and tested the
ErrorController itself to make sure it responds correctly when
environ['<app>.json_response'] is set.


Suggestions are still welcome ;)

-Kumar

_______________________________________________
Paste-users mailing list
[email protected]
http://webwareforpython.org/cgi-bin/mailman/listinfo/paste-users

Reply via email to