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:
|
||||
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
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue