Skip to content

Commit

Permalink
chore: error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyrxng committed Oct 23, 2024
1 parent 02d867d commit dd20345
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/handlers/comment-created-callback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -32,10 +32,10 @@ export async function issueCommentCreatedCallback(
}
logger.info(`Answer: ${answer}`, { tokenUsage });
const tokens = `\n\n<!--\n${JSON.stringify(tokenUsage, null, 2)}\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);
}
}
6 changes: 5 additions & 1 deletion src/handlers/comments.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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("#")) {
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/issue-fetching.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export async function fetchLinkedIssues(params: FetchParams) {
return { streamlinedComments: {}, linkedIssues: [], specAndBodies: {}, seen: new Set<string>() };
}
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) {
Expand Down
3 changes: 2 additions & 1 deletion src/helpers/issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -239,7 +240,7 @@ export async function pullReadmeFromRepoForIssue(params: FetchParams): Promise<s
readme = Buffer.from(response.data.content, "base64").toString();
}
} catch (error) {
throw new Error(`Error fetching README from repository: ${error}`);
throw logger.error(`Error fetching README from repository: ${error}`);
}
return readme;
}
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
// @ts-expect-error - no types found
import * as core from "@actions/core";
// @ts-expect-error - no types found
import * as github from "@actions/github";
import { Value } from "@sinclair/typebox/value";
import { envSchema } from "./types/env";
Expand Down

0 comments on commit dd20345

Please sign in to comment.