fix(roles): Map IntegrityError to BadRequest as this is an client error, no server error.
This commit is contained in:
parent
0a3da51b92
commit
ec7bf39666
|
@ -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
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue