Issue #86 new

Enhance rc.BAD_REQUEST, etc, to take text message

Anonymous created an issue

It is often useful to return an explanatory message along with errors. Currently this looks like:

{{{ response = rc.BAD_REQUEST response.write(" Invalid number format") return response }}}

Would be nice if you could pass the text to rc.BAD_REQUEST. Something like:

{{{ return rc.BAD_REQUEST(" Invalid number format") }}}

Same for the other error status codes (FORBIDDEN, NOT_IMPLEMENTED, etc)

Comments (3)

  1. James Fisher

    I wholeheartedly agree. It's my opinion that the rc.<ERROR> thing is actually undesirable altogether - Django already provides descriptive error codes, e.g. HttpResponseBadRequest. The only thing that piston does is set the content-type to text-plain.

    My approach to this would be to create a responses.py in piston, which provides response classes that can be used in exactly the same way as in Django, so you can do:

    from piston.responses import HttpResponseThrottled ... return HttpResponseThrottled('You seem to have been posting an awful lot for hours on end. The European Working Time Directive requires you take a break. Go make some coffee.')

    This should be pretty trivial to do, actually, and it means I can give more descriptive error messages in the actual piston code easily, too. I'll get onto it.

  2. Anonymous

    I'm pretty interested in getting this one implemented. Is anyone actively working on it or should I take a hack at it?

  3. Rock Howard

    What I did was to define an exception APIRequestError that takes a string and then I modified piston to catch that error at the appropriate place and generate the rc.BadRequest. So my handler has:

    try:
        some_function()
    except:
        raise APIRequestError( "Call to some_function() failed")
    

    and I get a response that matches what I would get from this:

    try:
        some_function()
    except:
        response = rc.BAD_REQUEST
        response.write(", " + "Call to some_function() failed")
        return response
    

    I am not convinced that my approach is better, but it works for me.

  4. Log in to comment