开发者

Error handling in Django-Piston

开发者 https://www.devze.com 2023-02-12 00:51 出处:网络
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-

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

精彩评论

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