Skip to content

Commit

Permalink
refactor(error-handling): improve error handling by removing try-catc…
Browse files Browse the repository at this point in the history
…h blocks

- Removed unnecessary try-catch blocks from `delete.ts`, `branch.ts`, `purge.ts`, `unique_note.ts`, and `upload.ts` by incorporating a new `EnveloppeErrors` class for error handling
- Enhanced `Logs` class with a `writeToLog` method for improved error logging
- Introduced `EnveloppeErrors` class for consistent error management
- Updated `token_path.ts` to use `EnveloppeErrors` for error handling
- Adjusted error handling in `main.ts` by adding event listeners for unhandled rejections and errors
  • Loading branch information
Mara-Li committed Dec 29, 2024
1 parent b5a7d2e commit 9514567
Show file tree
Hide file tree
Showing 8 changed files with 290 additions and 292 deletions.
85 changes: 42 additions & 43 deletions src/GitHub/branch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import i18next from "i18next";
import { Notice } from "obsidian";
import { FilesManagement } from "src/GitHub/files";
import type Enveloppe from "src/main";
import type { Logs } from "../utils/logs";
import { EnveloppeErrors, type Logs } from "../utils/logs";

export class GithubBranch extends FilesManagement {
console: Logs;
Expand Down Expand Up @@ -235,52 +235,51 @@ export class GithubBranch extends FilesManagement {
async checkRepository(prop: Properties | Properties[], silent = true): Promise<void> {
prop = Array.isArray(prop) ? prop : [prop];
for (const repo of prop) {
try {
const repoExist = await this.octokit
.request("GET /repos/{owner}/{repo}", {
owner: repo.owner,
repo: repo.repo,
})
.catch((e) => {
//check the error code
if (e.status === 404) {
new Notice(i18next.t("commands.checkValidity.inRepo.error404", { repo }));
} else if (e.status === 403) {
new Notice(i18next.t("commands.checkValidity.inRepo.error403", { repo }));
} else if (e.status === 301) {
new Notice(i18next.t("commands.checkValidity.inRepo.error301", { repo }));
}
});
//@ts-ignore
if (repoExist.status === 200) {
this.console.info(
i18next.t("commands.checkValidity.repoExistsTestBranch", { repo })
);

const branchExist = await this.findMainBranch(repo);
if (!branchExist) {
const errorMsg = new Error(
i18next.t("commands.checkValidity.inBranch.error404", {
repo,
})
const repoExist = await this.octokit
.request("GET /repos/{owner}/{repo}", {
owner: repo.owner,
repo: repo.repo,
})
.catch((e) => {
//check the error code
if (e.status === 404) {
new Notice(i18next.t("commands.checkValidity.inRepo.error404", { repo }));
throw new EnveloppeErrors(
i18next.t("commands.checkValidity.inRepo.error404", { repo })
);
this.console.fatal(errorMsg);

break;
}
this.console.info(i18next.t("commands.checkValidity.success", { repo }));
if (!silent) {
this.console.noticeSuccess(
i18next.t("commands.checkValidity.success", { repo })
} else if (e.status === 403) {
new Notice(i18next.t("commands.checkValidity.inRepo.error403", { repo }));
throw new EnveloppeErrors(
i18next.t("commands.checkValidity.inRepo.error403", { repo })
);
} else if (e.status === 301) {
new Notice(i18next.t("commands.checkValidity.inRepo.error301", { repo }));
throw new EnveloppeErrors(
i18next.t("commands.checkValidity.inRepo.error301", { repo })
);
}
});
//@ts-ignore
if (repoExist.status === 200) {
this.console.info(
i18next.t("commands.checkValidity.repoExistsTestBranch", { repo })
);

const branchExist = await this.findMainBranch(repo);
if (!branchExist) {
throw new EnveloppeErrors(
i18next.t("commands.checkValidity.inBranch.error404", {
repo,
}),
{ cause: "Branch not found" }
);
}
this.console.info(i18next.t("commands.checkValidity.success", { repo }));
if (!silent) {
this.console.noticeSuccess(
i18next.t("commands.checkValidity.success", { repo })
);
}
} catch (e) {
const err = new Error(i18next.t("commands.checkValidity.error"));
err.stack = (e as Error).stack;
this.console.fatal(err);
this.console.info(e);
break;
}
}
}
Expand Down
97 changes: 40 additions & 57 deletions src/GitHub/delete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import type { FilesManagement } from "src/GitHub/files";
import { trimObject } from "src/utils";
import { isAttachment, verifyRateLimitAPI } from "src/utils/data_validation_test";
import { frontmatterSettingsRepository } from "src/utils/parse_frontmatter";
import type Enveloppe from "../main";

/**
* Delete file from github, based on a list of file in the original vault
Expand Down Expand Up @@ -122,34 +121,30 @@ async function deleteFromGithubOneRepo(
const isNeedToBeDeleted = isInObsidian ? isMarkdownForAnotherRepo : true;
if (isNeedToBeDeleted) {
const checkingIndex = file.file.contains(settings.upload.folderNote.rename)
? await checkIndexFiles(octokit, plugin, file.file, repo)
? await checkIndexFiles(octokit, file.file, repo)
: false;
try {
if (!checkingIndex) {
pconsole.trace(
`trying to delete file : ${file.file} from ${repo.owner}/${repo.repo}`
);
const response = await octokit.request(
"DELETE /repos/{owner}/{repo}/contents/{path}",
{
owner: repo.owner,
repo: repo.repo,
path: file.file,
message: `DELETE FILE : ${file.file}`,
sha: file.sha,
branch: branchName,
}
);
if (response.status === 200) {
deletedSuccess++;
result.deleted.push(file.file);
} else {
deletedFailed++;
result.undeleted.push(file.file);
if (!checkingIndex) {
pconsole.trace(
`trying to delete file : ${file.file} from ${repo.owner}/${repo.repo}`
);
const response = await octokit.request(
"DELETE /repos/{owner}/{repo}/contents/{path}",
{
owner: repo.owner,
repo: repo.repo,
path: file.file,
message: `DELETE FILE : ${file.file}`,
sha: file.sha,
branch: branchName,
}
);
if (response.status === 200) {
deletedSuccess++;
result.deleted.push(file.file);
} else {
deletedFailed++;
result.undeleted.push(file.file);
}
} catch (e) {
pconsole.fatal(e as Error);
}
}
}
Expand Down Expand Up @@ -248,48 +243,36 @@ function parseYamlFrontmatter(contents: string): unknown {
* - autoClean: false
* - share: false
* @param {Octokit} octokit GitHub API
* @param plugin
* @param {string} path path of the file to check
* @param {Properties} prop repository informations
* @return {Promise<boolean>} true if the file must be deleted
*/

async function checkIndexFiles(
octokit: Octokit,
plugin: Enveloppe,
path: string,
prop: Properties
): Promise<boolean> {
try {
const fileRequest = await octokit.request(
"GET /repos/{owner}/{repo}/contents/{path}",
{
owner: prop.owner,
repo: prop.repo,
path,
}
const fileRequest = await octokit.request("GET /repos/{owner}/{repo}/contents/{path}", {
owner: prop.owner,
repo: prop.repo,
path,
});
if (fileRequest.status === 200) {
// @ts-ignore
const fileContent = Base64.decode(fileRequest.data.content);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fileFrontmatter = parseYamlFrontmatter(fileContent) as any;
// if not share => don't delete
// Key preventing deletion :
// - index: true
// - delete: false
// return true for NO DELETION
return (
fileFrontmatter.index === "true" ||
fileFrontmatter.delete === "false" ||
!fileFrontmatter.share
);
if (fileRequest.status === 200) {
// @ts-ignore
const fileContent = Base64.decode(fileRequest.data.content);
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const fileFrontmatter = parseYamlFrontmatter(fileContent) as any;
// if not share => don't delete
// Key preventing deletion :
// - index: true
// - delete: false
// return true for NO DELETION
return (
fileFrontmatter.index === "true" ||
fileFrontmatter.delete === "false" ||
!fileFrontmatter.share
);
}
} catch (e) {
if (!(e instanceof DOMException)) {
plugin.console.fatal(e as Error);
return false;
}
}
return false;
}
Expand Down
Loading

0 comments on commit 9514567

Please sign in to comment.