Improve error handling for menu data fetching and update HTML structure: Enhance error logging in menuDataController, provide user-friendly error messages, and streamline the index.html file by adding new script references while removing an unused image.
This commit is contained in:
@@ -1,24 +1,22 @@
|
||||
const { MenuItem } = require('../models');
|
||||
const { withDbRetries } = require('./withDbRetries');
|
||||
|
||||
async function fetchMenuData() {
|
||||
try {
|
||||
const menuItems = await MenuItem.findAll({
|
||||
order: [['order_id', 'ASC']],
|
||||
include: [{
|
||||
model: MenuItem,
|
||||
as: 'submenu',
|
||||
required: false,
|
||||
order: [['order_id', 'ASC']]
|
||||
}]
|
||||
});
|
||||
|
||||
const menuData = buildMenuStructure(menuItems);
|
||||
|
||||
return menuData;
|
||||
} catch (error) {
|
||||
console.error('There was a problem fetching the menu data:', error);
|
||||
return [];
|
||||
}
|
||||
return withDbRetries(
|
||||
async () => {
|
||||
const menuItems = await MenuItem.findAll({
|
||||
order: [['order_id', 'ASC']],
|
||||
include: [{
|
||||
model: MenuItem,
|
||||
as: 'submenu',
|
||||
required: false,
|
||||
order: [['order_id', 'ASC']]
|
||||
}]
|
||||
});
|
||||
return buildMenuStructure(menuItems);
|
||||
},
|
||||
{ attempts: 3, baseDelayMs: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
function buildMenuStructure(menuItems) {
|
||||
|
||||
51
utils/withDbRetries.js
Normal file
51
utils/withDbRetries.js
Normal file
@@ -0,0 +1,51 @@
|
||||
const sequelize = require('../config/database');
|
||||
|
||||
const DEFAULTS = {
|
||||
attempts: 3,
|
||||
baseDelayMs: 200,
|
||||
};
|
||||
|
||||
function delay(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
/**
|
||||
* Führt eine DB-Operation bis zu `attempts` mal aus. Nach einem Fehler:
|
||||
* kurzes Backoff, dann Verbindung mit authenticate() neu prüfen (frische
|
||||
* Verbindung aus dem Pool), erneuter Versuch.
|
||||
* Kein sequelize.close() — würde die gemeinsame Instanz für alle Requests killen.
|
||||
*/
|
||||
async function withDbRetries(operation, options = {}) {
|
||||
const { attempts, baseDelayMs } = { ...DEFAULTS, ...options };
|
||||
let lastError;
|
||||
|
||||
for (let attempt = 1; attempt <= attempts; attempt++) {
|
||||
try {
|
||||
if (attempt > 1) {
|
||||
const backoff = baseDelayMs * 2 ** (attempt - 2);
|
||||
await delay(backoff);
|
||||
try {
|
||||
await sequelize.authenticate();
|
||||
} catch (authErr) {
|
||||
console.warn(
|
||||
`DB authenticate vor Wiederholung ${attempt}/${attempts} fehlgeschlagen:`,
|
||||
authErr.message
|
||||
);
|
||||
}
|
||||
}
|
||||
return await operation();
|
||||
} catch (error) {
|
||||
lastError = error;
|
||||
console.error(
|
||||
`DB-Operation Versuch ${attempt}/${attempts} fehlgeschlagen:`,
|
||||
error.message
|
||||
);
|
||||
if (attempt === attempts) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
throw lastError;
|
||||
}
|
||||
|
||||
module.exports = { withDbRetries };
|
||||
Reference in New Issue
Block a user