开发者

Error handling in Django-Piston

In Django-Piston, is there a good way to do error handling? (Like returning a 400 status code when the caller omits a required GET parameter, or when the param开发者_JAVA百科eters are invalid.)


Django-piston respects HTTP status codes and handles common erros by default (auth, etc), but you can also throw new exceptions or status using rc from piston.utils.

For example:

from django.contrib.auth.models import User
from piston.handler import AnonymousBaseHandler
from piston.utils import rc

class AnonymousUserHandler(AnonymousBaseHandler):
    allowed_methods = ('GET', )
    fields = ('id', 'username',)

    def read(self, request):
        try:
            user = User.objects.get(username=request.GET.get('username', None))
            return user
        except Exception:
            resp = rc.NOT_FOUND
            resp.write(' User not found')
            return resp

Check out all utilities at https://bitbucket.org/jespern/django-piston/wiki/Documentation#!helpers-utils-decorators

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜