From 0ff2f3189ea5aea74303138b531994026222d524 Mon Sep 17 00:00:00 2001 From: Navan Chauhan Date: Thu, 24 Sep 2020 16:57:10 +0530 Subject: added exception handling for unknown hostname --- api.py | 5 ++++- app/misc.ini | 1 + app/views.py | 8 +++++++- 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/api.py b/api.py index db7a881..e7ac8cd 100644 --- a/api.py +++ b/api.py @@ -19,13 +19,16 @@ def gen_word(N, min_N_dig, min_N_low): dbFailing = False import mysql.connector as con -from mysql.connector.errors import InterfaceError +from mysql.connector.errors import InterfaceError, DatabaseError try: mycon = con.connect(host=flask_app.config['DB_HOST'],user=flask_app.config['DB_USER'],password=flask_app.config['DB_PASSWORD'],port=flask_app.config['DB_PORT'],database=flask_app.config['DB_NAME']) mycursor = mycon.cursor() except InterfaceError: print("Could not connect to the database!") dbFailing = True +except DatabaseError: + print("Unknown Host") + dbFailing = True """ @flask_app.route("/") diff --git a/app/misc.ini b/app/misc.ini index be76896..62c2cc4 100644 --- a/app/misc.ini +++ b/app/misc.ini @@ -9,6 +9,7 @@ PL01 = Failed to fetch the PDB, please check the PDB Code DB00 = Failed to connect to the database. DB01 = This Job ID does not exist 😠. If you think this is an error, please contact us. +DB02 = Unknown Hostname, failed to connect to the database. CW01 = Looks like an invalid PDB ID diff --git a/app/views.py b/app/views.py index 5b105ed..47a37ac 100644 --- a/app/views.py +++ b/app/views.py @@ -15,7 +15,7 @@ import json import subprocess import mysql.connector as con -from mysql.connector.errors import InterfaceError +from mysql.connector.errors import InterfaceError,DatabaseError import requests @@ -134,6 +134,8 @@ def status(): mycursor = mycon.cursor() except InterfaceError: return render_template('error.html',code="DB00",description=errors['DB00']) + except DatabaseError: + return render_template('error.html',code="DB02",description=errors['DB02']) sqlQuery = 'select id, protein_name, ligand_name, date, description, done, pdb from curieweb where id="%s"' % (jobID) mycursor.execute(sqlQuery) records = mycursor.fetchall() @@ -309,6 +311,8 @@ def dock_manual(): mycursor = mycon.cursor() except InterfaceError: return render_template("error.html",code="DB00",description=errors['DB00']) + except DatabaseError: + return render_template("error.html",code="DB02",description=errors['DB02']) import tempfile with tempfile.TemporaryDirectory() as directory: @@ -361,6 +365,8 @@ def dock_automatic(): mycursor = mycon.cursor() except InterfaceError: return render_template('error.html',code="DB00",description=errors['DB00']) + except DatabaseError: + return render_template("error.html",code="DB02",description=errors['DB02']) sqlQuery = "insert into curieweb (id, email, pdb, ligand_smile, ligand_name, date, description) values (%s,%s,%s,%s,%s,CURDATE(),%s) " jobID = gen_word(16, 1, 1) -- cgit v1.2.3