From 347315f17192c9875b46cef042ac30aaf258b2b3 Mon Sep 17 00:00:00 2001 From: Shivaditya Shivganesh Date: Wed, 20 Nov 2024 12:11:35 -0500 Subject: [PATCH] fix: error handling --- src/adapters/supabase/helpers/comment.ts | 10 +++++----- src/adapters/supabase/helpers/supabase.ts | 4 ++-- src/handlers/add-comments.ts | 6 ++---- src/handlers/add-issue.ts | 3 ++- src/handlers/issue-deduplication.ts | 6 +++--- src/handlers/update-comments.ts | 4 ++-- src/handlers/update-issue.ts | 3 ++- 7 files changed, 18 insertions(+), 18 deletions(-) diff --git a/src/adapters/supabase/helpers/comment.ts b/src/adapters/supabase/helpers/comment.ts index 3d94fc5..3b1137d 100644 --- a/src/adapters/supabase/helpers/comment.ts +++ b/src/adapters/supabase/helpers/comment.ts @@ -28,10 +28,10 @@ export class Comment extends SuperSupabase { //First Check if the comment already exists const { data, error } = await this.supabase.from("issue_comments").select("*").eq("id", commentNodeId); if (error) { - throw new Error("Error creating comment"); + this.context.logger.error("Error creating comment"); } if (data && data.length > 0) { - throw new Error("Comment already exists"); + this.context.logger.error("Comment already exists"); return; } else { //Create the embedding for this comment @@ -46,7 +46,7 @@ export class Comment extends SuperSupabase { .from("issue_comments") .insert([{ id: commentNodeId, markdown, plaintext, author_id: authorId, payload, embedding: embedding, issue_id: issueId }]); if (error) { - throw new Error("Error creating comment: " + error.message); + this.context.logger.error("Error creating comment: " + error.message); } } this.context.logger.info("Comment created successfully"); @@ -78,7 +78,7 @@ export class Comment extends SuperSupabase { .update({ markdown, plaintext, embedding: embedding, payload, modified_at: new Date() }) .eq("id", commentNodeId); if (error) { - throw new Error("Error updating comment: " + error.message); + this.context.logger.error("Error updating comment: " + error.message); } } } @@ -94,7 +94,7 @@ export class Comment extends SuperSupabase { async deleteComment(commentNodeId: string) { const { error } = await this.supabase.from("issue_comments").delete().eq("id", commentNodeId); if (error) { - throw new Error("Error deleting comment: " + error.message); + this.context.logger.error("Error deleting comment: " + error.message); } } } diff --git a/src/adapters/supabase/helpers/supabase.ts b/src/adapters/supabase/helpers/supabase.ts index 3f8268d..b40616a 100644 --- a/src/adapters/supabase/helpers/supabase.ts +++ b/src/adapters/supabase/helpers/supabase.ts @@ -16,8 +16,8 @@ export class SuperSupabase { if (!error) { return true; } else { - console.log(error); - throw new Error("Error connecting to Supabase or Schema has not been migrated/created"); + this.context.logger.error("Error connecting to Supabase or Schema has not been migrated/created"); + return false; } } } diff --git a/src/handlers/add-comments.ts b/src/handlers/add-comments.ts index 94ac384..0b2644b 100644 --- a/src/handlers/add-comments.ts +++ b/src/handlers/add-comments.ts @@ -16,10 +16,10 @@ export async function addComments(context: Context) { try { if (!markdown) { - throw new Error("Comment body is empty"); + logger.error("Comment body is empty"); } if (context.payload.issue.pull_request) { - throw new Error("Comment is on a pull request"); + logger.error("Comment is on a pull request"); } if ((await supabase.issue.getIssue(issueId)) === null) { await addIssue(context); @@ -30,10 +30,8 @@ export async function addComments(context: Context) { } catch (error) { if (error instanceof Error) { logger.error(`Error creating comment:`, { error: error, stack: error.stack }); - throw error; } else { logger.error(`Error creating comment:`, { err: error, error: new Error() }); - throw error; } } logger.debug(`Exiting addComments`); diff --git a/src/handlers/add-issue.ts b/src/handlers/add-issue.ts index c828bbf..c897021 100644 --- a/src/handlers/add-issue.ts +++ b/src/handlers/add-issue.ts @@ -15,7 +15,8 @@ export async function addIssue(context: Context) { try { if (!markdown) { - throw new Error("Issue body is empty"); + logger.error("Issue body is empty"); + return; } const cleanedIssue = removeFootnotes(markdown); await supabase.issue.createIssue(nodeId, payload, isPrivate, cleanedIssue, authorId); diff --git a/src/handlers/issue-deduplication.ts b/src/handlers/issue-deduplication.ts index b667a7b..83be4b0 100644 --- a/src/handlers/issue-deduplication.ts +++ b/src/handlers/issue-deduplication.ts @@ -92,7 +92,7 @@ function matchRepoOrgToSimilarIssueRepoOrg(repoOrg: string, similarIssueRepoOrg: * @param similarIssueContent The content of the similar issue * @returns The most similar sentence and its similarity score */ -function findMostSimilarSentence(issueContent: string, similarIssueContent: string): { sentence: string; similarity: number; index: number } { +function findMostSimilarSentence(issueContent: string, similarIssueContent: string, context: Context): { sentence: string; similarity: number; index: number } { // Regex to match sentences while preserving URLs const sentenceRegex = /([^.!?\s][^.!?]*(?:[.!?](?!['"]?\s|$)[^.!?]*)*[.!?]?['"]?(?=\s|$))/g; // Function to split text into sentences while preserving URLs @@ -128,7 +128,7 @@ function findMostSimilarSentence(issueContent: string, similarIssueContent: stri }); if (!mostSimilarSentence) { - throw new Error("No similar sentence found"); + context.logger.error("No similar sentence found"); } return { sentence: mostSimilarSentence, similarity: maxSimilarity, index: mostSimilarIndex }; } @@ -231,7 +231,7 @@ async function processSimilarIssues(similarIssues: IssueSimilaritySearchResult[] { issueNodeId: issue.issue_id } ); issueUrl.similarity = Math.round(issue.similarity * 100).toString(); - issueUrl.mostSimilarSentence = findMostSimilarSentence(issueBody, issueUrl.node.body); + issueUrl.mostSimilarSentence = findMostSimilarSentence(issueBody, issueUrl.node.body, context); return issueUrl; }) ); diff --git a/src/handlers/update-comments.ts b/src/handlers/update-comments.ts index cd585ed..1a8ff22 100644 --- a/src/handlers/update-comments.ts +++ b/src/handlers/update-comments.ts @@ -17,10 +17,10 @@ export async function updateComment(context: Context) { // Fetch the previous comment and update it in the db try { if (!markdown) { - throw new Error("Comment body is empty"); + logger.error("Comment body is empty"); } if (context.payload.issue.pull_request) { - throw new Error("Comment is on a pull request"); + logger.error("Comment is on a pull request"); } if ((await supabase.issue.getIssue(issueId)) === null) { await addIssue(context); diff --git a/src/handlers/update-issue.ts b/src/handlers/update-issue.ts index cec5de2..8fe8fad 100644 --- a/src/handlers/update-issue.ts +++ b/src/handlers/update-issue.ts @@ -16,7 +16,8 @@ export async function updateIssue(context: Context) { // Fetch the previous issue and update it in the db try { if (!markdown) { - throw new Error("Issue body is empty"); + logger.error("Issue body is empty"); + return; } //clean issue by removing footnotes const cleanedIssue = removeFootnotes(markdown);