Skip to content

Commit

Permalink
fix: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
sshivaditya committed Nov 20, 2024
1 parent 98c58cb commit 347315f
Show file tree
Hide file tree
Showing 7 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/adapters/supabase/helpers/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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");
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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);
}
}
}
4 changes: 2 additions & 2 deletions src/adapters/supabase/helpers/supabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
}
6 changes: 2 additions & 4 deletions src/handlers/add-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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`);
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/add-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/handlers/issue-deduplication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 };
}
Expand Down Expand Up @@ -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;
})
);
Expand Down
4 changes: 2 additions & 2 deletions src/handlers/update-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 2 additions & 1 deletion src/handlers/update-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 347315f

Please sign in to comment.