diff --git a/src/handlers/shared/start.ts b/src/handlers/shared/start.ts index d1e5a1c..8433e5b 100644 --- a/src/handlers/shared/start.ts +++ b/src/handlers/shared/start.ts @@ -18,7 +18,7 @@ export async function start( const { taskStaleTimeoutDuration } = config; if (!sender) { - throw new Error(logger.error(`Skipping '/start' since there is no sender in the context.`).logMessage.raw); + throw logger.error(`Skipping '/start' since there is no sender in the context.`); } // is it a child issue? @@ -27,7 +27,7 @@ export async function start( context, "```diff\n# Please select a child issue from the specification checklist to work on. The '/start' command is disabled on parent issues.\n```" ); - throw new Error(logger.error(`Skipping '/start' since the issue is a parent issue`).logMessage.raw); + throw logger.error(`Skipping '/start' since the issue is a parent issue`); } let commitHash: string | null = null; @@ -46,7 +46,7 @@ export async function start( // is it assignable? if (issue.state === ISSUE_TYPE.CLOSED) { - throw new Error(logger.error("This issue is closed, please choose another.", { issueNumber: issue.number }).logMessage.raw); + throw logger.error("This issue is closed, please choose another.", { issueNumber: issue.number }); } const assignees = issue?.assignees ?? []; @@ -54,11 +54,9 @@ export async function start( // find out if the issue is already assigned if (assignees.length !== 0) { const isCurrentUserAssigned = !!assignees.find((assignee) => assignee?.login === sender.login); - throw new Error( - logger.error( - isCurrentUserAssigned ? "You are already assigned to this task." : "This issue is already assigned. Please choose another unassigned task.", - { issueNumber: issue.number } - ).logMessage.raw + throw logger.error( + isCurrentUserAssigned ? "You are already assigned to this task." : "This issue is already assigned. Please choose another unassigned task.", + { issueNumber: issue.number } ); } @@ -81,7 +79,7 @@ export async function start( } if (error) { - throw new Error(logger.error(error, { issueNumber: issue.number }).logMessage.raw); + throw logger.error(error, { issueNumber: issue.number }); } // get labels @@ -89,7 +87,7 @@ export async function start( const priceLabel = labels.find((label: Label) => label.name.startsWith("Price: ")); if (!priceLabel) { - throw new Error(logger.error("No price label is set to calculate the duration", { issueNumber: issue.number }).logMessage.raw); + throw logger.error("No price label is set to calculate the duration", { issueNumber: issue.number }); } const deadline = getDeadline(labels); @@ -161,7 +159,7 @@ async function handleTaskLimitChecks(username: string, context: Context, logger: } if (await hasUserBeenUnassigned(context, username)) { - throw new Error(logger.error(`${username} you were previously unassigned from this task. You cannot be reassigned.`, { username }).logMessage.raw); + throw logger.error(`${username} you were previously unassigned from this task. You cannot be reassigned.`, { username }); } return true; diff --git a/src/handlers/shared/stop.ts b/src/handlers/shared/stop.ts index af41cf6..9bd57ff 100644 --- a/src/handlers/shared/stop.ts +++ b/src/handlers/shared/stop.ts @@ -17,7 +17,7 @@ export async function stop( const userToUnassign = assignees.find((assignee: Partial) => assignee?.login?.toLowerCase() === sender.login.toLowerCase()); if (!userToUnassign) { - throw new Error(logger.error("You are not assigned to this task", { issueNumber, user: sender.login })?.logMessage.raw as string); + throw logger.error("You are not assigned to this task", { issueNumber, user: sender.login }); } // close PR diff --git a/src/plugin.ts b/src/plugin.ts index e027d4f..8369351 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -43,12 +43,10 @@ export async function startStopTask(inputs: PluginInputs, env: Env) { let errorMessage; if (err instanceof LogReturn) { errorMessage = err; - } else if (err instanceof Error) { - errorMessage = context.logger.error(err.message, { error: err }); + await addCommentToIssue(context, `${errorMessage?.logMessage.diff}\n`); } else { - errorMessage = context.logger.error("An error occurred", { err }); + context.logger.error("An error occurred", { err }); } - await addCommentToIssue(context, `${errorMessage?.logMessage.diff}\n`); } } diff --git a/src/utils/issue.ts b/src/utils/issue.ts index adfc310..65a094e 100644 --- a/src/utils/issue.ts +++ b/src/utils/issue.ts @@ -1,4 +1,4 @@ -import type { RestEndpointMethodTypes } from "@octokit/plugin-rest-endpoint-methods/dist-types/generated/parameters-and-response-types"; +import { RestEndpointMethodTypes } from "@octokit/rest"; import ms from "ms"; import { Context } from "../types/context"; import { GitHubIssueSearch, Review } from "../types/payload"; @@ -142,9 +142,7 @@ async function confirmMultiAssignment(context: Context, issueNumber: number, use }); if (!assignees?.length) { - throw new Error( - logger.error("We detected that this task was not assigned to anyone. Please report this to the maintainers.", { issueNumber, usernames }).logMessage.raw - ); + throw logger.error("We detected that this task was not assigned to anyone. Please report this to the maintainers.", { issueNumber, usernames }); } if (isPrivate && assignees?.length <= 1) { diff --git a/tests/main.test.ts b/tests/main.test.ts index dec87fe..41e7e94 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -141,7 +141,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("You are not assigned to this task"); + await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "You are not assigned to this task" } }); }); test("User can't stop an issue without assignees", async () => { @@ -151,7 +151,7 @@ describe("User start/stop", () => { const context = createContext(issue, sender, "/stop") as Context<"issue_comment.created">; context.adapters = createAdapters(getSupabase(), context as unknown as Context); - await expect(userStartStop(context)).rejects.toThrow("You are not assigned to this task"); + await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "You are not assigned to this task" } }); }); test("User can't start an issue that's already assigned", async () => { @@ -162,7 +162,9 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("This issue is already assigned. Please choose another unassigned task."); + await expect(userStartStop(context)).rejects.toMatchObject({ + logMessage: { raw: "This issue is already assigned. Please choose another unassigned task." }, + }); }); test("User can't start an issue without a price label", async () => { @@ -173,7 +175,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("No price label is set to calculate the duration"); + await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "No price label is set to calculate the duration" } }); }); test("User can't start an issue without a wallet address", async () => { @@ -194,7 +196,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context as unknown as Context); - await expect(userStartStop(context)).rejects.toThrow("This issue is closed, please choose another."); + await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "This issue is closed, please choose another." } }); }); test("User can't start an issue that's a parent issue", async () => { @@ -205,7 +207,7 @@ describe("User start/stop", () => { context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("Skipping '/start' since the issue is a parent issue"); + await expect(userStartStop(context)).rejects.toMatchObject({ logMessage: { raw: "Skipping '/start' since the issue is a parent issue" } }); }); test("should set maxLimits to 6 if the user is a member", async () => { @@ -218,7 +220,9 @@ describe("User start/stop", () => { const context = createContext(issue, sender) as unknown as Context; context.adapters = createAdapters(getSupabase(), context as unknown as Context); - await expect(userStartStop(context)).rejects.toThrow("You have reached your max task limit. Please close out some tasks before assigning new ones."); + await expect(userStartStop(context)).rejects.toMatchObject({ + logMessage: { raw: "You have reached your max task limit. Please close out some tasks before assigning new ones." }, + }); expect(memberLimit).toEqual(6); }); @@ -230,7 +234,9 @@ describe("User start/stop", () => { const context = createContext(issue, sender, "/start") as Context<"issue_comment.created">; context.adapters = createAdapters(getSupabase(), context); - await expect(userStartStop(context)).rejects.toThrow("user2 you were previously unassigned from this task. You cannot be reassigned."); + await expect(userStartStop(context)).rejects.toMatchObject({ + logMessage: { raw: "user2 you were previously unassigned from this task. You cannot be reassigned." }, + }); }); test("Should throw if no BOT_USER_ID is set", async () => {