mirror of
https://github.com/tiennm99/store-scraper-bot.git
synced 2026-10-11 03:13:50 +00:00
refactor: remove redundant inner try/catch in commands
The dispatcher already wraps every handler in try/catch and sends the
'Internal server error' fallback on failure. Each command's inner
try/catch around its Redis ops was masking that path — the dispatcher
never saw the error, so logger.error('command failed') never fired.
Removing the inner catches restores observability and shortens each
file. The semantically-different try/catch blocks (mapping upstream
API failures to a different user message) are kept.
This commit is contained in:
1 parent
3f586d1d9f
commit
413dc62d4c
10 files changed
+73
-113
No files matched your search
@@ -28,18 +28,14 @@ export function createAddAppleAppCommand(store, appleScraper) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const added = await store.group.addAppleApp(msg.chat.id, resp.appId, country);
|
||||
if (!added) {
|
||||
await sender.sendMessage(msg.chat.id, `Apple app <code>${resp.appId}</code> is already added`);
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(
|
||||
msg.chat.id,
|
||||
`Apple app <code>${resp.appId}</code>, country <b>${country}</b> added successfully`,
|
||||
);
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
const added = await store.group.addAppleApp(msg.chat.id, resp.appId, country);
|
||||
if (!added) {
|
||||
await sender.sendMessage(msg.chat.id, `Apple app <code>${resp.appId}</code> is already added`);
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(
|
||||
msg.chat.id,
|
||||
`Apple app <code>${resp.appId}</code>, country <b>${country}</b> added successfully`,
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -23,18 +23,14 @@ export function createAddGoogleAppCommand(store, googleScraper) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const added = await store.group.addGoogleApp(msg.chat.id, appId, country);
|
||||
if (!added) {
|
||||
await sender.sendMessage(msg.chat.id, `Google app <code>${appId}</code> is already added`);
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(
|
||||
msg.chat.id,
|
||||
`Google app <code>${appId}</code>, country <b>${country}</b> added successfully`,
|
||||
);
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
const added = await store.group.addGoogleApp(msg.chat.id, appId, country);
|
||||
if (!added) {
|
||||
await sender.sendMessage(msg.chat.id, `Google app <code>${appId}</code> is already added`);
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(
|
||||
msg.chat.id,
|
||||
`Google app <code>${appId}</code>, country <b>${country}</b> added successfully`,
|
||||
);
|
||||
};
|
||||
}
|
||||
@@ -17,16 +17,12 @@ export function createAddGroupCommand(config, store) {
|
||||
}
|
||||
groupId = parsed;
|
||||
}
|
||||
try {
|
||||
const added = await store.admin.addGroup(groupId);
|
||||
if (!added) {
|
||||
await sender.sendMessage(msg.chat.id, 'Group is already added');
|
||||
return;
|
||||
}
|
||||
await store.group.initGroup(groupId);
|
||||
await sender.sendMessage(msg.chat.id, 'Group added successfully');
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
const added = await store.admin.addGroup(groupId);
|
||||
if (!added) {
|
||||
await sender.sendMessage(msg.chat.id, 'Group is already added');
|
||||
return;
|
||||
}
|
||||
await store.group.initGroup(groupId);
|
||||
await sender.sendMessage(msg.chat.id, 'Group added successfully');
|
||||
};
|
||||
}
|
||||
@@ -9,21 +9,17 @@ export function createCheckAppScoresCommand(store, appleScraper, googleScraper)
|
||||
await sender.sendMessage(msg.chat.id, 'Invalid arguments');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const group = await store.group.getGroup(msg.chat.id);
|
||||
const headers = ['AppId', 'Score', 'Ratings'];
|
||||
const appleRows = await scoreRowsFor(group.appleApps, appleScraper);
|
||||
const googleRows = await scoreRowsFor(group.googleApps, googleScraper);
|
||||
const group = await store.group.getGroup(msg.chat.id);
|
||||
const headers = ['AppId', 'Score', 'Ratings'];
|
||||
const appleRows = await scoreRowsFor(group.appleApps, appleScraper);
|
||||
const googleRows = await scoreRowsFor(group.googleApps, googleScraper);
|
||||
|
||||
const out =
|
||||
'<b>Apple Apps</b>\n' +
|
||||
renderTable(appleRows, headers) +
|
||||
'\n<b>Google Apps</b>\n' +
|
||||
renderTable(googleRows, headers);
|
||||
await sender.sendMessage(msg.chat.id, out);
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
}
|
||||
const out =
|
||||
'<b>Apple Apps</b>\n' +
|
||||
renderTable(appleRows, headers) +
|
||||
'\n<b>Google Apps</b>\n' +
|
||||
renderTable(googleRows, headers);
|
||||
await sender.sendMessage(msg.chat.id, out);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -10,24 +10,20 @@ export function createCheckAppCommand(config, store, appleScraper, googleScraper
|
||||
await sender.sendMessage(msg.chat.id, 'Invalid arguments');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const group = await store.group.getGroup(msg.chat.id);
|
||||
const nowMs = Date.now();
|
||||
const threshold = config.numDaysWarningNotUpdated;
|
||||
const headers = ['AppId', 'Updated', 'Days', 'OK'];
|
||||
const group = await store.group.getGroup(msg.chat.id);
|
||||
const nowMs = Date.now();
|
||||
const threshold = config.numDaysWarningNotUpdated;
|
||||
const headers = ['AppId', 'Updated', 'Days', 'OK'];
|
||||
|
||||
const appleRows = await appleRowsFor(group.appleApps, appleScraper, nowMs, threshold, config.timezone);
|
||||
const googleRows = await googleRowsFor(group.googleApps, googleScraper, nowMs, threshold, config.timezone);
|
||||
const appleRows = await appleRowsFor(group.appleApps, appleScraper, nowMs, threshold, config.timezone);
|
||||
const googleRows = await googleRowsFor(group.googleApps, googleScraper, nowMs, threshold, config.timezone);
|
||||
|
||||
const out =
|
||||
'<b>Apple Apps</b>\n' +
|
||||
renderTable(appleRows, headers) +
|
||||
'\n<b>Google Apps</b>\n' +
|
||||
renderTable(googleRows, headers);
|
||||
await sender.sendMessage(msg.chat.id, out);
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
}
|
||||
const out =
|
||||
'<b>Apple Apps</b>\n' +
|
||||
renderTable(appleRows, headers) +
|
||||
'\n<b>Google Apps</b>\n' +
|
||||
renderTable(googleRows, headers);
|
||||
await sender.sendMessage(msg.chat.id, out);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -8,15 +8,11 @@ export function createDeleteAppleAppCommand(store) {
|
||||
await sender.sendMessage(msg.chat.id, 'Invalid arguments');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const removed = await store.group.removeAppleApp(msg.chat.id, args[0]);
|
||||
if (!removed) {
|
||||
await sender.sendMessage(msg.chat.id, 'Apple app is not added');
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(msg.chat.id, 'Apple app deleted successfully');
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
const removed = await store.group.removeAppleApp(msg.chat.id, args[0]);
|
||||
if (!removed) {
|
||||
await sender.sendMessage(msg.chat.id, 'Apple app is not added');
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(msg.chat.id, 'Apple app deleted successfully');
|
||||
};
|
||||
}
|
||||
@@ -8,15 +8,11 @@ export function createDeleteGoogleAppCommand(store) {
|
||||
await sender.sendMessage(msg.chat.id, 'Invalid arguments');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const removed = await store.group.removeGoogleApp(msg.chat.id, args[0]);
|
||||
if (!removed) {
|
||||
await sender.sendMessage(msg.chat.id, 'Google app is not added');
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(msg.chat.id, 'Google app deleted successfully');
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
const removed = await store.group.removeGoogleApp(msg.chat.id, args[0]);
|
||||
if (!removed) {
|
||||
await sender.sendMessage(msg.chat.id, 'Google app is not added');
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(msg.chat.id, 'Google app deleted successfully');
|
||||
};
|
||||
}
|
||||
@@ -17,15 +17,11 @@ export function createDeleteGroupCommand(config, store) {
|
||||
}
|
||||
groupId = parsed;
|
||||
}
|
||||
try {
|
||||
const removed = await store.admin.removeGroup(groupId);
|
||||
if (!removed) {
|
||||
await sender.sendMessage(msg.chat.id, 'Group is not added');
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(msg.chat.id, 'Group deleted successfully');
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
const removed = await store.admin.removeGroup(groupId);
|
||||
if (!removed) {
|
||||
await sender.sendMessage(msg.chat.id, 'Group is not added');
|
||||
return;
|
||||
}
|
||||
await sender.sendMessage(msg.chat.id, 'Group deleted successfully');
|
||||
};
|
||||
}
|
||||
@@ -9,17 +9,13 @@ export function createListAppCommand(store) {
|
||||
await sender.sendMessage(msg.chat.id, 'Invalid arguments');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const group = await store.group.getGroup(msg.chat.id);
|
||||
const out =
|
||||
'<b>Apple Apps</b>\n' +
|
||||
formatAppTable(group.appleApps) +
|
||||
'\n<b>Google Apps</b>\n' +
|
||||
formatAppTable(group.googleApps);
|
||||
await sender.sendMessage(msg.chat.id, out);
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
}
|
||||
const group = await store.group.getGroup(msg.chat.id);
|
||||
const out =
|
||||
'<b>Apple Apps</b>\n' +
|
||||
formatAppTable(group.appleApps) +
|
||||
'\n<b>Google Apps</b>\n' +
|
||||
formatAppTable(group.googleApps);
|
||||
await sender.sendMessage(msg.chat.id, out);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -8,17 +8,13 @@ export function createListGroupCommand(config, store) {
|
||||
await sender.sendMessage(msg.chat.id, 'Invalid arguments');
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const groups = await store.admin.getAllGroups();
|
||||
if (groups.length === 0) {
|
||||
await sender.sendMessage(msg.chat.id, 'No groups found');
|
||||
return;
|
||||
}
|
||||
const lines = [`<b>Authorized groups (${groups.length}):</b>`];
|
||||
groups.forEach((gid, i) => lines.push(`${i + 1}. <code>${gid}</code>`));
|
||||
await sender.sendMessage(msg.chat.id, lines.join('\n') + '\n');
|
||||
} catch {
|
||||
await sender.sendMessage(msg.chat.id, 'Internal server error');
|
||||
const groups = await store.admin.getAllGroups();
|
||||
if (groups.length === 0) {
|
||||
await sender.sendMessage(msg.chat.id, 'No groups found');
|
||||
return;
|
||||
}
|
||||
const lines = [`<b>Authorized groups (${groups.length}):</b>`];
|
||||
groups.forEach((gid, i) => lines.push(`${i + 1}. <code>${gid}</code>`));
|
||||
await sender.sendMessage(msg.chat.id, lines.join('\n') + '\n');
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user