Skip to content

Commit c26c0bd

Browse files
committed
docs: added error handling design rules
1 parent 5488a96 commit c26c0bd

File tree

5 files changed

+63
-33
lines changed

5 files changed

+63
-33
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,31 @@ Feedr requires Bun in order to work
4646
2. Fill out all the required values in `.env.example` and rename it to `.env` once done
4747
3. To run in developer mode, just run `bun --watch . --dev`, otherwise `bun run .`
4848

49+
## Design Rules
50+
51+
These rules are what to follow when working and developing on Feedr. There aren't a lot, but important for error handling.
52+
53+
### Database Function Return Guidelines
54+
55+
Each database function should **always** return a success indicator (`true`/`false`) along with associated data. To avoid confusion, here are the expected return types:
56+
57+
- **Success with data:** `true` should always return populated data, even if the data is not used. For example:
58+
```ts
59+
return { success: true, data: Data as Data };
60+
```
61+
62+
- **Success without data:** `true` can also indicate a successful operation where no data is returned. In this case, an empty array (`[]`) should be provided:
63+
```ts
64+
return { success: true, data: [] };
65+
```
66+
67+
- **Failure:** `false` should indicate an error or unsuccessful operation. This should always return an empty array (`[]`) to ensure consistency:
68+
```ts
69+
return { success: false, data: [] };
70+
```
71+
72+
These guidelines ensure predictable behavior and simplify error handling across the application.
73+
4974
# Changelog
5075

5176
## 1.4.0

src/commands.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import { PermissionFlagsBits } from "discord-api-types/v8";
1616
import checkIfChannelIdIsValid from "./utils/youtube/checkIfChannelIdIsValid";
1717
import {
1818
addNewGuildToTrackChannel,
19-
checkIfGuildIsTrackingChannelAlready,
2019
getAllTrackedInGuild,
2120
stopGuildTrackingChannel,
2221
twitchAddNewChannelToTrack,
@@ -32,6 +31,8 @@ import {
3231
addNewChannelToTrack,
3332
} from "./utils/db/youtube";
3433
import search from "./utils/youtube/search";
34+
import { checkIfGuildIsTrackingUserAlready } from "./utils/db/discord";
35+
import { Platform } from "./types/types.d";
3536

3637
import client from ".";
3738

@@ -363,13 +364,15 @@ const commands: Record<string, Command> = {
363364
}
364365

365366
const trackedChannels =
366-
await checkIfGuildIsTrackingChannelAlready(
367+
await checkIfGuildIsTrackingUserAlready(
368+
Platform.YouTube,
367369
platformUserId,
368370
guildId,
369371
);
370372

371373
// Check if the channel is already being tracked in the guild
372-
if (trackedChannels.length) {
374+
console.log(trackedChannels);
375+
if (trackedChannels) {
373376
await interaction.reply({
374377
flags: MessageFlags.Ephemeral,
375378
content: `This channel is already being tracked in ${trackedChannels.map((channel, index) => `${index > 0 && index === trackedChannels.length - 1 ? "and " : ""}<#${channel.guild_channel_id}>`).join(", ")}!`,
@@ -434,7 +437,7 @@ const commands: Record<string, Command> = {
434437
}
435438

436439
const trackedChannels =
437-
await checkIfGuildIsTrackingChannelAlready(
440+
await checkIfGuildIsTrackingUserAlready(
438441
platformUserId,
439442
guildId,
440443
);
@@ -624,7 +627,7 @@ const commands: Record<string, Command> = {
624627
case "youtube":
625628
// Check if the channel is not being tracked in the guild
626629
if (
627-
!(await checkIfGuildIsTrackingChannelAlready(
630+
!(await checkIfGuildIsTrackingUserAlready(
628631
youtubeChannelId,
629632
guildId,
630633
))
@@ -673,7 +676,7 @@ const commands: Record<string, Command> = {
673676

674677
// check if the channel is not being tracked in the guild
675678
if (
676-
!(await checkIfGuildIsTrackingChannelAlready(
679+
!(await checkIfGuildIsTrackingUserAlready(
677680
streamerId,
678681
guildId,
679682
))

src/utils/database.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,26 +27,6 @@ export const pool: Pool = new Pool({
2727
});
2828

2929
// #region YouTube
30-
export async function checkIfGuildIsTrackingChannelAlready(
31-
channelId: string,
32-
guild_id: string,
33-
): Promise<dbDiscordTable[]> {
34-
const query = `SELECT * FROM discord WHERE platform_user_id = ? AND guild_id = ?`;
35-
36-
try {
37-
const statement = db.prepare(query);
38-
const result = statement.all(channelId, guild_id);
39-
40-
return result;
41-
} catch (err) {
42-
console.error(
43-
"Error checking if guild is tracking channel already:",
44-
err,
45-
);
46-
throw err;
47-
}
48-
}
49-
5030
export async function addNewGuildToTrackChannel(
5131
guild_id: string,
5232
channelId: string,

src/utils/db/audit.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
export enum AuditResolution {
2+
AUDIT_RESOLUTION_FAIL = "fail",
3+
AUDIT_RESOLUTION_SUCCESS = "success",
4+
AUDIT_RESOLUTION_ERROR = "error",
5+
AUDIT_RESOLUTION_NULL = "null",
6+
}
7+
8+
export enum AuditType {
9+
GUILD_CHECKED_YOUTUBE_CHANNEL = "guild_checked_youtube_channel",
10+
}
11+
12+
export async function addAuditEntry(
13+
type: AuditType,
14+
resolution: AuditResolution,
15+
) {
16+
console.log("Adding audit entry:", type, resolution);
17+
throw new Error("Not implemented");
18+
}

src/utils/db/discord.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import { Platform } from "../../types/types";
1+
import { Platform } from "../../types/types.d";
22
import { pool } from "../database";
33

44
export async function checkIfGuildIsTrackingUserAlready(
55
platform: Platform,
66
userId: string,
77
guildId: string,
8-
): Promise<boolean> {
8+
): Promise<{ success: boolean; data: any[] | null }> {
99
console.log(
1010
`Checking if guild ${guildId} is tracking user ${userId} on platform ${platform}`,
1111
);
@@ -15,19 +15,19 @@ export async function checkIfGuildIsTrackingUserAlready(
1515
if (platform === Platform.YouTube) {
1616
query = `
1717
SELECT * FROM guild_youtube_subscriptions
18-
WHERE youtube_channel_id = ? AND guild_id = ?
18+
WHERE youtube_channel_id = $1 AND guild_id = $2
1919
`;
2020
} else if (platform === Platform.Twitch) {
2121
query = `
2222
SELECT * FROM guild_twitch_subscriptions
23-
WHERE twitch_user_id = ? AND guild_id = ?
23+
WHERE twitch_user_id = $1 AND guild_id = $2
2424
`;
2525
}
2626

2727
if (!query) {
2828
console.error("Invalid platform provided for tracking check.");
2929

30-
return false;
30+
return { success: false, data: null };
3131
}
3232

3333
try {
@@ -36,10 +36,14 @@ export async function checkIfGuildIsTrackingUserAlready(
3636

3737
client.release();
3838

39-
return result.rows.length > 0;
39+
if (result.rows.length > 0) {
40+
return { success: true, data: result.rows };
41+
} else {
42+
return { success: false, data: null };
43+
}
4044
} catch (error) {
4145
console.error("Error checking if guild is tracking user:", error);
4246

43-
return false;
47+
return { success: false, data: null };
4448
}
4549
}

0 commit comments

Comments
 (0)