django-tastypie has immediatehttpresponse
exception allow return client immediate response:
raise immediatehttpresponse(response='a message')
django has http404, couldn't find more universal exception immediatehttpresponse
.
what technique use return client immediate 400 response?
for example having model:
class subscriber(model): def delete(self, *args, **kwargs): raise immediatehttpresponse('deleting subcribers not allowed!')
and trying delete object return client 400
response given message.
i think want middleware implements process_exception.
it works this: raise exception on view (e.g. immediatehttpresponse). if exception catched middleware, middleware returns response, in case status 400. if don't catch exception, django catch in end, returning status 500 (server error), or other status.
the simplest example middleware catches http404. if catch exception http404 in middleware, can return response want (status 200, 400, etc). if don't catch (i.e. process_exception method of middleware returns none), django catch , return response status 404. standard way of having own custom exceptions want respond in custom way.
Comments
Post a Comment