From dd203454c146dcec38c738030d79b02e3781479e Mon Sep 17 00:00:00 2001 From: Keyrxng <106303466+Keyrxng@users.noreply.github.com> Date: Thu, 24 Oct 2024 00:17:44 +0100 Subject: [PATCH] chore: error handling --- src/handlers/comment-created-callback.ts | 8 ++++---- src/handlers/comments.ts | 6 +++++- src/helpers/errors.ts | 2 +- src/helpers/issue-fetching.ts | 2 +- src/helpers/issue.ts | 3 ++- src/main.ts | 2 ++ 6 files changed, 15 insertions(+), 8 deletions(-) diff --git a/src/handlers/comment-created-callback.ts b/src/handlers/comment-created-callback.ts index 471a0bc..b366f70 100644 --- a/src/handlers/comment-created-callback.ts +++ b/src/handlers/comment-created-callback.ts @@ -23,7 +23,7 @@ export async function issueCommentCreatedCallback( return { status: 204, reason: logger.info("Comment is empty. Skipping.").logMessage.raw }; } logger.info(`Asking question: ${question}`); - let commentToPost; + try { const response = await askQuestion(context, question); const { answer, tokenUsage } = response; @@ -32,10 +32,10 @@ export async function issueCommentCreatedCallback( } logger.info(`Answer: ${answer}`, { tokenUsage }); const tokens = `\n\n`; - commentToPost = answer + tokens; + const commentToPost = answer + tokens; await addCommentToIssue(context, commentToPost); return { status: 200, reason: logger.info("Comment posted successfully").logMessage.raw }; - } catch (err) { - throw err; + } catch (error) { + throw await bubbleUpErrorComment(context, error, false); } } diff --git a/src/handlers/comments.ts b/src/handlers/comments.ts index d7e2d0f..b033686 100644 --- a/src/handlers/comments.ts +++ b/src/handlers/comments.ts @@ -1,3 +1,4 @@ +import { logger } from "../helpers/errors"; import { splitKey } from "../helpers/issue"; import { LinkedIssues, SimplifiedComment } from "../types/github-types"; import { StreamlinedComment } from "../types/llm"; @@ -53,7 +54,10 @@ export function createKey(issueUrl: string, issue?: number) { } if (!key) { - throw new Error("Invalid issue url"); + throw logger.error("Invalid issue URL", { + issueUrl, + issueNumber: issue, + }); } if (key.includes("#")) { diff --git a/src/helpers/errors.ts b/src/helpers/errors.ts index 5372863..e53a425 100644 --- a/src/helpers/errors.ts +++ b/src/helpers/errors.ts @@ -16,7 +16,7 @@ export async function bubbleUpErrorComment(context: Context, err: unknown, post if (err instanceof LogReturn) { errorMessage = err; } else if (err instanceof Error) { - errorMessage = context.logger.error(err.message, { error: err }); + errorMessage = context.logger.error(err.message, { stack: err.stack }); } else { errorMessage = context.logger.error("An error occurred", { err }); } diff --git a/src/helpers/issue-fetching.ts b/src/helpers/issue-fetching.ts index c3d9249..744e74e 100644 --- a/src/helpers/issue-fetching.ts +++ b/src/helpers/issue-fetching.ts @@ -42,7 +42,7 @@ export async function fetchLinkedIssues(params: FetchParams) { return { streamlinedComments: {}, linkedIssues: [], specAndBodies: {}, seen: new Set() }; } if (!issue.body || !issue.html_url) { - throw logger.error("Issue body or URL not found"); + throw logger.error("Issue body or URL not found", { issueUrl: issue.html_url }); } if (!params.owner || !params.repo) { diff --git a/src/helpers/issue.ts b/src/helpers/issue.ts index 68fcda7..0effb4a 100644 --- a/src/helpers/issue.ts +++ b/src/helpers/issue.ts @@ -2,6 +2,7 @@ import { createKey } from "../handlers/comments"; import { FetchedCodes, FetchParams, LinkedIssues } from "../types/github-types"; import { StreamlinedComment } from "../types/llm"; import { Context } from "../types/context"; // Import Context type +import { logger } from "./errors"; /** * Removes duplicate streamlined comments based on their body content. @@ -239,7 +240,7 @@ export async function pullReadmeFromRepoForIssue(params: FetchParams): Promise