Skip to content

Commit

Permalink
better use of try/catch for DB access, less callbacks, see #208 and #209
Browse files Browse the repository at this point in the history
  • Loading branch information
jbphet committed May 25, 2019
1 parent 029788d commit fcbd356
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions js/routeHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' );
Expand Down

0 comments on commit fcbd356

Please sign in to comment.