Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Refactor: Make error handling rock solid #10

Merged
merged 1 commit into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/events/messageCreate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sleep } from "../utils/sleep";
import { ACTION } from "./action.types";

/**
* Handles the thread create event.
* Handles the message create event.
*
* @param {ExtendedClient} bot The bot's Discord instance.
* @param {Message} message The thread channel payload from Discord.
Expand Down
4 changes: 2 additions & 2 deletions src/events/messageDelete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sleep } from "../utils/sleep";
import { ACTION } from "./action.types";

/**
* Handles the thread create event.
* Handles the thread delete event.
*
* @param {ExtendedClient} bot The bot's Discord instance.
* @param {Message | PartialMessage} message The thread channel payload from Discord.
Expand All @@ -28,6 +28,6 @@ export const messageDelete = async (
await sendMessageToSupabase(ACTION.MESSAGE_DELETE, bot, message);
await sleep(1000);
} catch (err) {
await errorHandler(bot, "message create", err);
await errorHandler(bot, "message delete", err);
}
};
2 changes: 1 addition & 1 deletion src/events/messageUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { sleep } from "../utils/sleep";
import { ACTION } from "./action.types";

/**
* Handles the thread create event.
* Handles the message update event.
*
* @param {ExtendedClient} bot The bot's Discord instance.
* @param {Message | PartialMessage} message The thread channel payload from Discord.
Expand Down
79 changes: 48 additions & 31 deletions src/utils/sendToSupabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Database } from "../database.types";
import { ACTION } from "../events/action.types";
import { ExtendedClient } from "../interfaces/ExtendedClient";

import { supabaseErrorTransformer } from "./supabaseErrorTransformer";
import {
messageTransform,
threadAction,
Expand Down Expand Up @@ -47,7 +48,7 @@ export const sendThreadToSupabase = async (
.insert([threadJson])
.select();
if (error) {
return Promise.reject(error);
throw new Error(supabaseErrorTransformer(error));
}
return data;
}
Expand All @@ -69,7 +70,7 @@ export const sendThreadToSupabase = async (
.select();

if (error) {
return Promise.reject(error);
throw new Error(supabaseErrorTransformer(error));
}

return data;
Expand All @@ -79,7 +80,9 @@ export const sendThreadToSupabase = async (
.from("community_raw")
.select("*")
.eq("origin", thread.id);

if (error) {
throw new Error(supabaseErrorTransformer(error));
}
if (data && data.length > 0) {
const { raw, ...rest } = data[0];
const deletedRawData = Object.assign(
Expand All @@ -98,14 +101,12 @@ export const sendThreadToSupabase = async (
.select();

if (deletePostError) {
return Promise.reject(deletePostError);
throw new Error(supabaseErrorTransformer(deletePostError));
}
return deletedPost;
} else {
throw new Error("Post not found while deleting.");
}
if (error) {
return Promise.reject(error);
}
return;
}
default: {
return;
Expand All @@ -116,13 +117,13 @@ export const sendThreadToSupabase = async (
/**
*
* @param {typeof ACTION} action - Create, update or delete.
* @param {ExtendedClient} bot - Bot instance.
* @param {ExtendedClient} _bot - Bot instance.
* @param {Message} message - Message instance.
* @returns {any} - Any.
*/
export const sendMessageToSupabase = async (
action: string,
bot: ExtendedClient,
_bot: ExtendedClient,
message: Message | PartialMessage
) => {
switch (action) {
Expand All @@ -134,7 +135,7 @@ export const sendMessageToSupabase = async (
.select("*")
.eq("origin", threadId);
if (error) {
return Promise.reject(error);
throw new Error(supabaseErrorTransformer(error));
}
if (threadData && threadData.length > 0) {
const { raw } = threadData[0];
Expand Down Expand Up @@ -164,13 +165,18 @@ export const sendMessageToSupabase = async (
.eq("origin", threadId)
.select();
if (createMessageError) {
return Promise.reject(createMessageError);
throw new Error(supabaseErrorTransformer(createMessageError));
}
return updatedPost;
}
// Do nothing
return;
} else {
throw new Error("Post not found while creating a message.");
}
} else {
throw new Error("Message doesn't belong to helpChannel");
}
return Promise.reject("Message doesn't belong to helpChannel");
}
case ACTION.MESSAGE_UPDATE: {
// Same as message create, let it be repeating
Expand All @@ -182,7 +188,7 @@ export const sendMessageToSupabase = async (
.select("*")
.eq("origin", threadId);
if (error) {
return Promise.reject(error);
throw new Error(supabaseErrorTransformer(error));
}
if (threadData && threadData.length > 0) {
const { raw } = threadData[0];
Expand All @@ -204,21 +210,24 @@ export const sendMessageToSupabase = async (
[]
);
if (!found) {
messages.push(transformedMessage);
const { data: updatedPost, error: createMessageError } =
await supabase
.from("community_raw")
.update({ raw: Object.assign({}, raw, { messages }) })
.eq("origin", threadId)
.select();
if (createMessageError) {
return Promise.reject(createMessageError);
}
return updatedPost;
throw new Error("Message not found while updating.");
}
const { data: updatedPost, error: updateMessageError } =
await supabase
.from("community_raw")
.update({ raw: Object.assign({}, raw, { messages }) })
.eq("origin", threadId)
.select();
if (updateMessageError) {
throw new Error(supabaseErrorTransformer(updateMessageError));
}
return updatedPost;
} else {
throw new Error("Post not found while updating a message.");
}
} else {
throw new Error("Message doesn't belong to helpChannel");
}
return Promise.reject("Message doesn't belong to helpChannel");
}
case ACTION.MESSAGE_DELETE: {
const threadId = message.channel.id;
Expand All @@ -228,15 +237,17 @@ export const sendMessageToSupabase = async (
.select("*")
.eq("origin", threadId);
if (error) {
return Promise.reject(error);
throw new Error(supabaseErrorTransformer(error));
}
if (threadData && threadData.length > 0) {
const { raw } = threadData[0];
let { messages } = JSON.parse(JSON.stringify(raw)) as TransformPost;
const transformedMessage = messageTransform(message);
let found = false;
messages = messages.reduce(
(msgs: TransformMessage[], m: TransformMessage) => {
if (m.id === transformedMessage.id) {
found = true;
// Transformed message can be partial, so we need the
// current values of message from raw
msgs.push({ ...m, ...transformedMessage, deleted: true });
Expand All @@ -247,19 +258,25 @@ export const sendMessageToSupabase = async (
},
[]
);
const { data: updatedPost, error: createMessageError } =
if (!found) {
throw new Error("Message not found while deleting.");
}
const { data: updatedPost, error: deleteMessageError } =
await supabase
.from("community_raw")
.update({ raw: Object.assign({}, raw, { messages }) })
.eq("origin", threadId)
.select();
if (createMessageError) {
return Promise.reject(createMessageError);
if (deleteMessageError) {
throw new Error(supabaseErrorTransformer(deleteMessageError));
}
return updatedPost;
} else {
throw new Error("Post not found while deleting a message.");
}
} else {
throw new Error("Message doesn't belong to helpChannel");
}
return Promise.reject("Message doesn't belong to helpChannel");
}
default: {
return;
Expand Down
12 changes: 12 additions & 0 deletions src/utils/supabaseErrorTransformer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { PostgrestError } from "@supabase/supabase-js";

/**
*
* @param {PostgrestError} error Error from supabase.
* @returns {string} String representation of error message with code.
*/
export const supabaseErrorTransformer = (error: PostgrestError) => {
const { code, message } = error;

return `Code: ${code}. Message: ${message}`;
};
2 changes: 2 additions & 0 deletions src/utils/transformers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,8 @@ export const messageTransform = (message: Message | PartialMessage) => {
};
messageJson.author = messageAuthorValue;
}
// Set to empty array same as attachments
messageJson.reactions = [];
for (const [, messageReactionValue] of message.reactions.cache) {
const messageReactionJson =
messageReactionValue.toJSON() as TransformMessageReaction;
Expand Down
Loading