fix(roles): Map IntegrityError to BadRequest as this is an client error, no server error.

This commit is contained in:
Ferdinand Thiessen 2021-12-18 01:51:38 +01:00
parent 0a3da51b92
commit ec7bf39666
1 changed files with 9 additions and 6 deletions

View File

@ -17,7 +17,7 @@ def get(role_name) -> Role:
else: else:
role = Role.query.filter(Role.name == role_name).one_or_none() role = Role.query.filter(Role.name == role_name).one_or_none()
if not role: if not role:
raise NotFound raise NotFound("no such role")
return role return role
@ -56,11 +56,14 @@ def create_permissions(permissions):
def create_role(name: str, permissions=[]): def create_role(name: str, permissions=[]):
logger.debug(f"Create new role with name: {name}") logger.debug(f"Create new role with name: {name}")
role = Role(name=name) try:
db.session.add(role) role = Role(name=name)
set_permissions(role, permissions) db.session.add(role)
db.session.commit() set_permissions(role, permissions)
logger.debug(f"Created role: {role.serialize()}") db.session.commit()
logger.debug(f"Created role: {role.serialize()}")
except IntegrityError:
raise BadRequest("role already exists")
return role return role