Skip to content
Snippets Groups Projects
Commit 8b78356e authored by Vytor Calixto's avatar Vytor Calixto :space_invader:
Browse files

Merge branch 'fix_database_error_reporting' into 'development'

Fix error handling in the database query module

Changes in this merge request
* Add proper error handling in libs/db/query_exec. Uses a catch function after each then to proper capture database errors and report them to the user.
* Add logging the error stack when a database query error occurs.
* Change error message reported in the response when a database query occurs, because the user making the request does not need to know about an internal database error.

See merge request !28
parents e5864f6f 427b110a
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!28Fix error handling in the database query module
Pipeline #
......@@ -37,16 +37,17 @@ app.use((req, res, next) => {
req.sql = squel.select();
next();
});
// Mounts all API routes under /api/v1
app.use('/api/v1', api);
// Catch 404 and forward to error handler
app.use((req, res, next) => {
res.status(404);
log.error('%s %d %s', req.method, res.statusCode, req.url);
res.json({ error: 'Not found' }).end();
res.json({ error: 'Error 404: Page not found' }).end();
});
// Error handlers
// Express' default error handler
app.use((err, req, res, next) => {
res.status(err.status || 500);
log.error('%s %d %s', req.method, res.statusCode, err.message);
......
......@@ -19,26 +19,23 @@ function execSqlQuery(sqlQuery, sqlQueryParams = []) {
log.debug(`Executing SQL query '${sqlQuery}' with params '${sqlQueryParams}'`);
return new Promise((resolve, reject) => {
// Prepare statement
conn.prepare(sqlQuery, true).then(
(dbQuery) => {
// Execute query
dbQuery.exec(sqlQueryParams).then(
// Success
(dbResult) => {
log.debug(`Query result: ${dbResult.data}`);
log.debug(dbResult.data);
resolve(dbResult.data);
},
// Error
(dbError) => {
log.error(`SQL query execution error: ${dbError.message}`);
reject(new Error(dbError.message));
}
);
// Release resources allocated for prepared statement
conn.release();
}
);
conn.prepare(sqlQuery, true).then((dbQuery) => {
// Execute query
dbQuery.exec(sqlQueryParams).then((dbResult) => {
log.debug(`Query result: ${dbResult.data}`);
// release resources allocated for the prepared statement
dbQuery.release();
resolve(dbResult.data);
}).catch((queryError) => {
log.error(`SQL query execution error: ${queryError.message}`);
// release resources allocated for the prepared statement
dbQuery.release();
reject(new Error(queryError.message));
});
}).catch((prepError) => {
log.error(`SQL prepared statement error: ${prepError.message}`);
reject(new Error(prepError.message));
});
});
}
......
......@@ -9,10 +9,13 @@ function query(req, res, next) {
execQuery(sql.text, sql.values).then((result) => {
log.debug(result);
req.result = result;
if(result.length === 0) next({status: 404, message: 'Not Found'});
if (result.length === 0) {
next({status: 404, message: 'Not Found'});
}
next();
}, (error) => {
next(error);
log.error(error.stack);
next(new Error('Request could not be satisfied due to a database error.'));
});
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment