Files
compass-core/compass/api/errors.py
grace.yu 4db268c7c4 Updated API and tests
Supported get clusters by querying deploy state
Fixed warning message "baseException.message deprecated in Python 2.6"
Add tests for modified API
Fixed one string concatenation bug
Change-Id: Ifec3854c286929b69bd689407b4ac409f520fac6
2014-02-06 14:30:11 -08:00

104 lines
2.6 KiB
Python

"""Exception and its handler"""
from compass.api import app
from compass.api import util
class ObjectDoesNotExist(Exception):
"""Define the exception for referring non-existing object"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class UserInvalidUsage(Exception):
"""Define the exception for fault usage of users"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class ObjectDuplicateError(Exception):
"""Define the duplicated object exception"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class InputMissingError(Exception):
"""Define the insufficient input exception"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class MethodNotAllowed(Exception):
"""Define the exception which invalid method is called"""
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
@app.errorhandler(ObjectDoesNotExist)
def handle_not_exist(error, failed_objs=None):
"""Handler of ObjectDoesNotExist Exception"""
message = {'status': 'Not Found',
'message': error.message}
if failed_objs and isinstance(failed_objs, dict):
message.update(failed_objs)
return util.make_json_response(404, message)
@app.errorhandler(UserInvalidUsage)
def handle_invalid_usage(error):
"""Handler of UserInvalidUsage Exception"""
message = {'status': 'Invalid parameters',
'message': error.message}
return util.make_json_response(400, message)
@app.errorhandler(InputMissingError)
def handle_mssing_input(error):
"""Handler of InputMissingError Exception"""
message = {'status': 'Insufficient data',
'message': error.message}
return util.make_json_response(400, message)
@app.errorhandler(ObjectDuplicateError)
def handle_duplicate_object(error, failed_objs=None):
"""Handler of ObjectDuplicateError Exception"""
message = {'status': 'Conflict Error',
'message': error.message}
if failed_objs and isinstance(failed_objs, dict):
message.update(failed_objs)
return util.make_json_response(409, message)
@app.errorhandler(MethodNotAllowed)
def handle_not_allowed_method(error):
"""Handler of MethodNotAllowed Exception"""
message = {"status": "Method Not Allowed",
"message": "The method is not allowed to use"}
return util.make_json_response(405, message)