Something like this?
class RestHandler(object):
# "default" exception handling in the super class
def handle_exception(self, e):
logger.error('Default error handling caught a "%s"' % e)
class RestRegisterHandler(RestHandler):
# "specific" exception handling in the child class
def handle_exception(self, e):
# Just an idea, you can implement this in different ways
WRITE_ERROR = (InvalidRequest, InvalidToken, DuplicateKeyError)
WRITE_ERROR_WITH_LOGGER = (Exception,)
RAISE_VALIDATION_ERROR = (ModelValidationError,)
if e in WRITE_ERROR:
self.write_error(404, str(e))
elif e in WRITE_ERROR_WITH_LOGGER:
logger.error(e)
self.write_error(404, str(e))
elif e in RAISE_VALIDATION_ERROR:
logger.error("Unknown Validation error: '{0}'".format(e))
raise utils.errors.ValidationError(400, error_messages=e.messages)
else:
# if couldn't be handled here, fallback to the default implementation
super(RestHandler, self).handle_exception(e)
@gen.coroutine
def post(self):
self.raw_data = None
try:
yield self.validate_user()
self.raw_data = json_decode(self.request.body)
logger.debug(self.raw_data)
model_object = self.model(self.raw_data)
model_object.validate()
logger.debug("Inseting to database")
yield model_object.insert(self.db)
except Exception as e:
self.handle_exception(e)
else:
logger.debug("db saved")
self.write("Registered succesfully")
return