From ec7bf396664d4ba57bcbd7be28422e0b7a3a1585 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Sat, 18 Dec 2021 01:51:38 +0100 Subject: [PATCH] fix(roles): Map IntegrityError to BadRequest as this is an client error, no server error. --- flaschengeist/controller/roleController.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/flaschengeist/controller/roleController.py b/flaschengeist/controller/roleController.py index c8b0cb2..204adad 100644 --- a/flaschengeist/controller/roleController.py +++ b/flaschengeist/controller/roleController.py @@ -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}") - role = Role(name=name) - db.session.add(role) - set_permissions(role, permissions) - db.session.commit() - logger.debug(f"Created role: {role.serialize()}") + 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