0
0
mirror of https://github.com/naturalcrit/homebrewery.git synced 2025-12-31 21:42:44 +00:00

proper error handling(i think)

This commit is contained in:
Víctor Losada Hernández
2024-02-15 15:50:27 +01:00
parent d7c9ab43bc
commit 62db393969
2 changed files with 42 additions and 22 deletions

View File

@@ -46,6 +46,10 @@ const archive = {
// No published documents found with the given title
return res.status(404).json({ error: 'Published documents not found' });
}
if (!brews || brews.length === 0) {
return res.status(404).json({ errorCode: '404', message: 'Published documents not found' });
}
const totalBrews = await HomebrewModel.countDocuments(titleQuery, projection);
const totalPages = Math.ceil(totalBrews / pageSize);
@@ -54,7 +58,11 @@ const archive = {
return res.json({ brews, page, totalPages, totalBrews});
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Internal Server Error' });
if (error.response && error.response.status === 503) {
return res.status(503).json({ errorCode: '503', message: 'Service Unavailable' });
} else {
return res.status(500).json({ errorCode: '500', message: 'Internal Server Error' });
}
}
}
};