picard/picard/models/validation_exception.py

43 lines
1.2 KiB
Python

import json
__author__ = 'tanshu'
from pyramid.response import Response
from sqlalchemy.exc import OperationalError, IntegrityError
import transaction
class ValidationError(Exception):
def __init__(self, header, message, Errors=None):
self.header = header
self.message = message
# Call the base class constructor with the parameters it needs
Exception.__init__(self, (header, message))
# Now for your custom code...
self.Errors = Errors
def __str__(self):
return self.message
def json(self):
return (self.header, self.message)
def TryCatchFunction(f):
def _decorator(self, *args, **kwargs):
try:
return f(self, *args, **kwargs)
except ValidationError as ex:
transaction.abort()
response = Response(json.dumps(ex.json()))
response.status_int = 500
return response
except (ValueError, KeyError, AttributeError, TypeError, OperationalError, IntegrityError) as ex:
transaction.abort()
response = Response(json.dumps(('Failed validation', str(ex))))
response.status_int = 500
return response
return _decorator