Below is a function that adds a new user to the database using their email and password. I am using Flask
and SQLAlchemy
.
@app.route('/users', methods = ['POST'])
@require_appkey
def registerUser():
errorsList = []
userInfo = request.get_json()
# Get "email" and "password"
email = userInfo.get('email')
password = userInfo.get('password')
if email is None or password is None:
errorsList.append(Error("email/password","Email or Password not entered"))
return make_error(MainError('400','Incorrect Input',errorsList))
# Check password meets criteria
if len(password) > 1024:
errorsList.append(Error("password","Password maximum length is 1024 characters"))
if len(password) < 8:
errorsList.append(Error("password","Password must be at least 8 characters"))
# Check email meets criteria
email = email.strip()
# Check if email address is valid (syntactically)
match = re.match("[^@]+@[^@]+\.[^@]+", email)
if match is None:
errorsList.append(Error("email","Not a valid email address"))
# Check if email address is already in database
with contextlib.closing(DBSession()) as session:
try:
if session.query(USER).filter_by(USERSEMAIL=email).count():
errorsList.append(Error("email","This email address already exists"))
except Exception as error:
session.rollback()
logger = logging.getLogger('__name__')
logger.error(error)
errorsList.append(Error("database","An error occurred with the database. Please try again later."))
return make_error(MainError('500','Database error',errorsList))
# If errors exist, return all errors
if errorsList:
return make_error(MainError('400','Incorrect Input',errorsList))
# Add user to database
user = USER(email, password)
with contextlib.closing(DBSession()) as session:
try:
session.add(user)
session.commit()
except Exception as error:
session.rollback()
logger = logging.getLogger('__name__')
logger.error(error)
errorsList.append(Error("database","An error occurred with the database. Please try again later."))
return make_error(MainError('500','Database error',errorsList))
return jsonify(data=user.serialize())
def make_error(mainError):
response = jsonify(data=mainError.serialize())
response.status_code = int(mainError.code)
return response
class MainError:
def __init__(self, code, message, errorsList):
self.code = code
self.message = message
# List of Error objects
self.errorsList = errorsList
def serialize(self):
return {
'errorCode': self.code,
'message': self.message,
'errorList': [error.serialize() for error in self.errorsList],
'success': 'false'
}
My specific questions (not all directly related to Python) are:
Should I be using
return
in so many different places?Is there a better way to structure my code? I would love to hear any thoughts you all have on how this could be written better.
Is the way in which I am handling my errors correct?
Seeing as I will probably be using the
Database
error often, is it worth putting into a function?
return
(rather thanraise
) an error that doesn't even inherit fromException
? And why ismake_error
separate from the class? \$\endgroup\$ – jonrsharpe Mar 30 '15 at 10:35