From fcbd35647dd563ee922a749f1d3c3541fa79acfc Mon Sep 17 00:00:00 2001 From: jbphet Date: Fri, 24 May 2019 18:01:02 -0600 Subject: [PATCH] better use of try/catch for DB access, less callbacks, see #208 and #209 --- js/routeHandlers.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/js/routeHandlers.js b/js/routeHandlers.js index ab5da82f..cf1bf757 100644 --- a/js/routeHandlers.js +++ b/js/routeHandlers.js @@ -340,21 +340,19 @@ module.exports.renderTranslationPage = async function( req, res ) { // create a query for retrieving the strings const savedStringsQuery = 'SELECT * from saved_translations where user_id = $1 AND locale = $2 AND (' + repositories + ')'; - await client.connect(); - - // query the database for saved strings corresponding to this user and sim - winston.log( 'info', 'running query: ' + savedStringsQuery ); + // connect to the database and query for saved strings corresponding to this user and sim let rows = null; try { + await client.connect(); + winston.log( 'info', 'running query: ' + savedStringsQuery ); const queryResponse = await client.query( savedStringsQuery, [ userId, targetLocale ] ); rows = queryResponse.rows; + client.end(); } catch( err ) { - winston.error( 'query of strings database failed, err = ' + err ); + winston.error( 'unable to retrieve saved strings from database, err = ' + err ); } - client.end(); - // load saved strings from database to saveStrings object if there are any if ( rows && rows.length > 0 ) { winston.log( 'info', 'using ' + rows.length + ' saved strings' );