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