Skip to content

Commit

Permalink
refactor: improve error handling in MilestoneClient
Browse files Browse the repository at this point in the history
  • Loading branch information
CalvinWilkinson committed Oct 29, 2023
1 parent f3254a0 commit 09dc423
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
9 changes: 9 additions & 0 deletions GitHubClients/Errors/MilestoneError.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
* Error thrown when there is an error with the milestone client.
*/
export class MilestoneError extends Error {
constructor(message: string) {
super(message);
this.name = "MilestoneError";
}
}
19 changes: 6 additions & 13 deletions GitHubClients/MilestoneClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { GitHubClient } from "../core/GitHubClient.ts";
import { IssueClient } from "./IssueClient.ts";
import { PullRequestClient } from "./PullRequestClient.ts";
import { IssueOrPR } from "../core/Types.ts";
import { MilestoneError } from "./Errors/MilestoneError.ts";

/**
* Provides a client for interacting with milestones.
Expand All @@ -28,7 +29,7 @@ export class MilestoneClient extends GitHubClient {
Guard.isNothing(ownerName, funcName, "ownerName");
Guard.isNothing(repoName, funcName, "this.repoName");

super(token);
super(ownerName, repoName, token);

this.issueClient = new IssueClient(ownerName, this.repoName, token);
this.prClient = new PullRequestClient(ownerName, this.repoName, token);
Expand Down Expand Up @@ -147,8 +148,7 @@ export class MilestoneClient extends GitHubClient {
if (milestone === undefined) {
const errorMsg = `The milestone '${milestoneName}' could not be found.`;

Utils.printError(errorMsg);
Deno.exit(1);
throw new MilestoneError(errorMsg)
}

return milestone;
Expand All @@ -175,8 +175,7 @@ export class MilestoneClient extends GitHubClient {
let errorMsg = `The milestones for the repository owner '${this.ownerName}'`;
errorMsg += ` and for the repository '${this.repoName}' could not be found.`;

Utils.printError(errorMsg);
Deno.exit(1);
throw new MilestoneError(errorMsg);
}

return [<MilestoneModel[]> await this.getResponseData(response), response];
Expand Down Expand Up @@ -224,17 +223,11 @@ export class MilestoneClient extends GitHubClient {
const response: Response = await this.requestPATCH(url, JSON.stringify({ state: "closed" }));

// If there is an error
if (response.status === GitHubHttpStatusCodes.OK) {
Utils.printAsGitHubNotice(`✅The milestone '${milestoneName}' has been closed.✅`);
} else if (response.status === GitHubHttpStatusCodes.NotFound) {
Utils.printError(`The organization '${this.ownerName}' or repo '${this.repoName}' does not exist.`);
Deno.exit(1);
} else {
if (response.status != GitHubHttpStatusCodes.OK) {
let errorMsg = `An error occurred trying to close milestone '${milestoneName}(${milestone.number})'.`;
errorMsg += `\nError: ${response.status}(${response.statusText})`;

Utils.printError(errorMsg);
Deno.exit(1);
throw new MilestoneError(errorMsg);
}
}
}

0 comments on commit 09dc423

Please sign in to comment.