Skip to content

Commit

Permalink
fix: error handling better logging
Browse files Browse the repository at this point in the history
  • Loading branch information
sshivaditya committed Nov 24, 2024
1 parent 347315f commit b193313
Show file tree
Hide file tree
Showing 11 changed files with 217 additions and 90 deletions.
89 changes: 67 additions & 22 deletions src/adapters/supabase/helpers/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,41 @@ export class Comment extends SuperSupabase {
issueId: string
) {
//First Check if the comment already exists
const { data, error } = await this.supabase.from("issue_comments").select("*").eq("id", commentNodeId);
if (error) {
const { data: existingData, error: existingError } = await this.supabase.from("issue_comments").select("*").eq("id", commentNodeId);
if (existingError) {
this.context.logger.error("Error creating comment");
}
if (data && data.length > 0) {
if (existingData && existingData.length > 0) {
this.context.logger.error("Comment already exists");
return;
} else {
//Create the embedding for this comment
const embedding = await this.context.adapters.voyage.embedding.createEmbedding(markdown);
let plaintext: string | null = markdownToPlainText(markdown);
if (isPrivate) {
markdown = null as string | null;
payload = null as Record<string, unknown> | null;
plaintext = null as string | null;
}
const { error } = await this.supabase
.from("issue_comments")
.insert([{ id: commentNodeId, markdown, plaintext, author_id: authorId, payload, embedding: embedding, issue_id: issueId }]);
if (error) {
this.context.logger.error("Error creating comment: " + error.message);
}
}
this.context.logger.info("Comment created successfully");
//Create the embedding for this comment
const embedding = await this.context.adapters.voyage.embedding.createEmbedding(markdown);
let plaintext: string | null = markdownToPlainText(markdown);
if (isPrivate) {
markdown = null as string | null;
payload = null as Record<string, unknown> | null;
plaintext = null as string | null;
}
const { data, error } = await this.supabase
.from("issue_comments")
.insert([{ id: commentNodeId, markdown, plaintext, author_id: authorId, payload, embedding: embedding, issue_id: issueId }]);
if (error) {
this.context.logger.error("Failed to create comment in database", {
Error: error,
commentData: {
id: commentNodeId,
markdown,
plaintext,
author_id: authorId,
payload,
embedding,
issue_id: issueId,
},
});
return;
}
this.context.logger.info(`Comment created successfully with id: ${commentNodeId}`, { data });
}

async updateComment(
Expand Down Expand Up @@ -78,23 +89,57 @@ export class Comment extends SuperSupabase {
.update({ markdown, plaintext, embedding: embedding, payload, modified_at: new Date() })
.eq("id", commentNodeId);
if (error) {
this.context.logger.error("Error updating comment: " + error.message);
this.context.logger.error("Error updating comment", {
Error: error,
commentData: {
id: commentNodeId,
markdown,
plaintext,
embedding,
payload,
modified_at: new Date(),
},
});
return;
}
this.context.logger.info("Comment updated successfully with id: " + commentNodeId, {
commentData: {
id: commentNodeId,
markdown,
plaintext,
embedding,
payload,
modified_at: new Date(),
},
});
}
}

async getComment(commentNodeId: string): Promise<CommentType[] | null> {
const { data, error } = await this.supabase.from("issue_comments").select("*").eq("id", commentNodeId);
if (error) {
this.context.logger.error("Error getting comment", error);
this.context.logger.error("Error getting comment", {
Error: error,
commentData: {
id: commentNodeId,
},
});
return null;
}
return data;
}

async deleteComment(commentNodeId: string) {
const { error } = await this.supabase.from("issue_comments").delete().eq("id", commentNodeId);
if (error) {
this.context.logger.error("Error deleting comment: " + error.message);
this.context.logger.error("Error deleting comment", {
Error: error,
commentData: {
id: commentNodeId,
},
});
return;
}
this.context.logger.info("Comment deleted successfully with id: " + commentNodeId);
}
}
149 changes: 116 additions & 33 deletions src/adapters/supabase/helpers/issues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,28 @@ export class Issues extends SuperSupabase {
//First Check if the issue already exists
const { data, error } = await this.supabase.from("issues").select("*").eq("id", issueNodeId);
if (error) {
throw new Error("Error creating issue: " + error.message);
this.context.logger.error("Error creating issue", {
Error: error,
issueData: {
id: issueNodeId,
payload,
isPrivate,
markdown,
authorId,
},
});
return;
}
if (data && data.length > 0) {
this.context.logger.info("Issue already exists");
this.context.logger.info("Issue already exists", {
issueData: {
id: issueNodeId,
payload,
isPrivate,
markdown,
authorId,
},
});
return;
} else {
const embedding = await this.context.adapters.voyage.embedding.createEmbedding(markdown, "document");
Expand All @@ -44,10 +62,28 @@ export class Issues extends SuperSupabase {
}
const { error } = await this.supabase.from("issues").insert([{ id: issueNodeId, payload, markdown, plaintext, author_id: authorId, embedding }]);
if (error) {
throw new Error("Error creating issue: " + error.message);
this.context.logger.error("Error creating issue: " + error.message, {
Error: error,
issueData: {
id: issueNodeId,
payload,
isPrivate,
markdown,
authorId,
},
});
return;
}
}
this.context.logger.info("Issue created successfully");
this.context.logger.info("Issue created successfully with id: " + issueNodeId, {
issueData: {
id: issueNodeId,
payload,
isPrivate,
markdown,
authorId,
},
});
}

async updateIssue(markdown: string | null, issueNodeId: string, payload: Record<string, unknown> | null, isPrivate: boolean, authorId: number) {
Expand All @@ -59,53 +95,100 @@ export class Issues extends SuperSupabase {
plaintext = null;
}
const issues = await this.getIssue(issueNodeId);
if (!issues) {
this.context.logger.info("Issue does not exist, creating a new one");
await this.createIssue(issueNodeId, payload, isPrivate, markdown, authorId);
} else {
const { error } = await this.supabase.from("issues").update({ markdown, plaintext, embedding, payload, modified_at: new Date() }).eq("id", issueNodeId);
if (error) {
throw new Error("Error updating issue: " + error.message);
try {
if (!issues) {
this.context.logger.info("Issue not found, creating new issue with id: " + issueNodeId);
await this.createIssue(issueNodeId, payload, isPrivate, markdown, authorId);
} else {
const { error } = await this.supabase.from("issues").update({ markdown, plaintext, embedding, payload, modified_at: new Date() }).eq("id", issueNodeId);
if (error) {
this.context.logger.error("Error updating issue", {
Error: error,
issueData: {
id: issueNodeId,
payload,
isPrivate,
markdown,
authorId,
},
});
}
}
} catch (error) {
this.context.logger.error("Error updating issue", {
Error: error,
issueData: {
id: issueNodeId,
payload,
isPrivate,
markdown,
authorId,
},
});
}
}

async deleteIssue(issueNodeId: string) {
const { error } = await this.supabase.from("issues").delete().eq("id", issueNodeId);
if (error) {
throw new Error("Error deleting issue: " + error.message);
this.context.logger.error("Error deleting issue: " + error.message, {
Error: error,
issueData: {
id: issueNodeId,
},
});
return;
}
this.context.logger.info("Issue deleted successfully with id: " + issueNodeId, {
issueData: {
id: issueNodeId,
},
});
}

async getIssue(issueNodeId: string): Promise<IssueType[] | null> {
const { data, error } = await this.supabase
.from("issues") // Provide the second type argument
.select("*")
.eq("id", issueNodeId)
.returns<IssueType[]>();
const { data, error } = await this.supabase.from("issues").select("*").eq("id", issueNodeId).returns<IssueType[]>();
if (error || !data || data.length === 0) {
if (error) {
this.context.logger.error("Error getting issue", error);
} else {
this.context.logger.error("Error getting issue: No data found");
}
this.context.logger.error("Error getting issue", {
Error: error,
issueData: {
id: issueNodeId,
},
});
return null;
}
return data;
}

async findSimilarIssues(markdown: string, threshold: number, currentId: string): Promise<IssueSimilaritySearchResult[] | null> {
const embedding = await this.context.adapters.voyage.embedding.createEmbedding(markdown, "query");
const { data, error } = await this.supabase.rpc("find_similar_issues", {
current_id: currentId,
query_embedding: embedding,
threshold: threshold,
top_k: 5,
});
if (error) {
this.context.logger.error("Error finding similar issues", error);
async findSimilarIssues(markdown: string, threshold: number, currentId: string): Promise<IssueSimilaritySearchResult[]> {
try {
const embedding = await this.context.adapters.voyage.embedding.createEmbedding(markdown, "query");
const { data, error } = await this.supabase.rpc("find_similar_issues", {
current_id: currentId,
query_embedding: embedding,
threshold,
top_k: 5,
});

if (error) {
this.context.logger.error("Error finding similar issues", {
Error: error,
markdown,
threshold,
currentId,
});
return [];
}

return data;
} catch (error) {
this.context.logger.error("Error finding similar issues", {
Error: error,
markdown,
threshold,
currentId,
});
return [];
}
return data;
}
}
3 changes: 1 addition & 2 deletions src/adapters/voyage/helpers/embedding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { VoyageAIClient } from "voyageai";
import { Context } from "../../../types";
import { SuperVoyage } from "./voyage";
import { EmbedRequestInputType } from "voyageai/api/types/EmbedRequestInputType";
const VECTOR_SIZE = 1024;

export class Embedding extends SuperVoyage {
protected context: Context;
Expand All @@ -14,7 +13,7 @@ export class Embedding extends SuperVoyage {

async createEmbedding(text: string | null, inputType: EmbedRequestInputType = "document"): Promise<number[]> {
if (text === null) {
return new Array(VECTOR_SIZE).fill(0);
throw new Error("Text is null");
} else {
const response = await this.client.embed({
input: text,
Expand Down
15 changes: 8 additions & 7 deletions src/handlers/add-comments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export async function addComments(context: Context) {
adapters: { supabase },
} = context;
const { payload } = context as { payload: CommentPayload };
const markdown = payload.comment.body;
const authorId = payload.comment.user?.id || -1;
const nodeId = payload.comment.node_id;
const comment = payload.comment;
const markdown = comment.body;
const authorId = comment.user?.id || -1;
const nodeId = comment.node_id;
const isPrivate = payload.repository.private;
const issueId = payload.issue.node_id;

Expand All @@ -22,16 +23,16 @@ export async function addComments(context: Context) {
logger.error("Comment is on a pull request");
}
if ((await supabase.issue.getIssue(issueId)) === null) {
logger.info("Parent issue not found, creating new issue");
await addIssue(context);
}
await supabase.comment.createComment(markdown, nodeId, authorId, payload, isPrivate, issueId);
logger.ok(`Created Comment with id: ${nodeId}`);
logger.ok(`Successfully created comment!`);
logger.ok(`Successfully created comment!`, comment);
} catch (error) {
if (error instanceof Error) {
logger.error(`Error creating comment:`, { error: error, stack: error.stack });
logger.error(`Error creating comment:`, { error: error, stack: error.stack, comment: comment });
} else {
logger.error(`Error creating comment:`, { err: error, error: new Error() });
logger.error(`Error creating comment:`, { err: error, comment: comment });
}
}
logger.debug(`Exiting addComments`);
Expand Down
14 changes: 7 additions & 7 deletions src/handlers/add-issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ export async function addIssue(context: Context) {
adapters: { supabase },
} = context;
const { payload } = context as { payload: IssuePayload };
const markdown = payload.issue.body + " " + payload.issue.title || null;
const authorId = payload.issue.user?.id || -1;
const nodeId = payload.issue.node_id;
const issue = payload.issue;
const markdown = issue.body + " " + issue.title || null;
const authorId = issue.user?.id || -1;
const nodeId = issue.node_id;
const isPrivate = payload.repository.private;

try {
Expand All @@ -20,16 +21,15 @@ export async function addIssue(context: Context) {
}
const cleanedIssue = removeFootnotes(markdown);
await supabase.issue.createIssue(nodeId, payload, isPrivate, cleanedIssue, authorId);
logger.ok(`Successfully created issue!`, issue);
} catch (error) {
if (error instanceof Error) {
logger.error(`Error creating issue:`, { error: error, stack: error.stack });
logger.error(`Error creating issue:`, { error: error, issue: issue });
throw error;
} else {
logger.error(`Error creating issue:`, { err: error, error: new Error() });
logger.error(`Error creating issue:`, { err: error, issue: issue });
throw error;
}
}

logger.ok(`Successfully created issue!`);
logger.debug(`Exiting addIssue`);
}
Loading

0 comments on commit b193313

Please sign in to comment.