Skip to content
Snippets Groups Projects
Commit 399320f0 authored by João Victor Tozatti Risso's avatar João Victor Tozatti Risso
Browse files

Merge branch 'fix_database_error_reporting' into...

Merge branch 'fix_database_error_reporting' into 71-simcaq-implementar-backend-uc201-selecionar-localidade
parents 5bdeca3d 427b110a
No related branches found
No related tags found
2 merge requests!116Release v1.0.0,!27Implement UC201 - Select Location
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