2

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 parameters are invalid.)

Nick Heiner
  • 119,074
  • 188
  • 476
  • 699

1 Answers1

4

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://django-piston-sheepdog.readthedocs.io/en/latest/helpers.html

Jonathan Hartley
  • 15,462
  • 9
  • 79
  • 80
goldstein
  • 467
  • 5
  • 9