From 9d80cb5564798f40936ac49a480ac243b882238f Mon Sep 17 00:00:00 2001 From: Luna Date: Mon, 4 Mar 2019 01:26:53 -0300 Subject: [PATCH] admin_api.voice: handle UniqueViolationError --- litecord/blueprints/admin_api/voice.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/litecord/blueprints/admin_api/voice.py b/litecord/blueprints/admin_api/voice.py index 4d26488..b6e123a 100644 --- a/litecord/blueprints/admin_api/voice.py +++ b/litecord/blueprints/admin_api/voice.py @@ -17,11 +17,13 @@ along with this program. If not, see . """ +import asyncpg from quart import Blueprint, jsonify, current_app as app, request from litecord.auth import admin_check from litecord.schemas import validate from litecord.admin_schemas import VOICE_SERVER, VOICE_REGION +from litecord.errors import BadRequest bp = Blueprint('voice_admin', __name__) @@ -58,10 +60,13 @@ async def put_region_server(region): await admin_check() j = validate(await request.get_json(), VOICE_SERVER) - await app.db.execute(""" - INSERT INTO voice_servers (hostname, region) - VALUES ($1, $2) - """, j['hostname'], region) + try: + await app.db.execute(""" + INSERT INTO voice_servers (hostname, region_id) + VALUES ($1, $2) + """, j['hostname'], region) + except asyncpg.UniqueViolationError: + raise BadRequest('voice server already exists with given hostname') return '', 204