From 7b9fca8d0061cf5e5af08cba98e9d5b6dbbed8ec Mon Sep 17 00:00:00 2001 From: HampusM Date: Wed, 21 Jul 2021 22:00:04 +0200 Subject: Began with better backend error handling & cleaned up the backend --- packages/server/src/git/branch.ts | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) (limited to 'packages/server/src/git/branch.ts') diff --git a/packages/server/src/git/branch.ts b/packages/server/src/git/branch.ts index 90241a0..163eca7 100644 --- a/packages/server/src/git/branch.ts +++ b/packages/server/src/git/branch.ts @@ -1,10 +1,15 @@ import { CommitSummary } from "./commit"; import { Reference } from "./reference"; import { Repository } from "./repository"; +import { Repository as NodeGitRepository } from "nodegit"; +import { BranchError, createError } from "./error"; export class Branch extends Reference { public async latestCommit(): Promise { - const latest_commit = this._owner.nodegitRepository.getBranchCommit(this._ng_reference); + const latest_commit = this._owner.nodegitRepository.getBranchCommit(this._ng_reference).catch(() => { + throw(createError(BranchError, 500, "Failed to get the latest commit")); + }); + return { id: (await latest_commit).sha(), message: (await latest_commit).message(), @@ -12,13 +17,19 @@ export class Branch extends Reference { }; } - public static async lookup(owner: Repository, branch: string): Promise { + public static async lookup(owner: Repository, branch: string): Promise { const reference = await owner.nodegitRepository.getBranch(branch).catch(err => { if(err.errno === -3) { - return null; + throw(createError(BranchError, 404, "Branch not found!")); } - throw(err); + throw(createError(BranchError, 500, "Something went wrong.")); }); - return reference ? new Branch(owner, reference) : null; + return new Branch(owner, reference); + } + + public static async lookupExists(ng_repository: NodeGitRepository, branch: string): Promise { + return ng_repository.getBranch(branch) + .then(() => true) + .catch(() => false); } } \ No newline at end of file -- cgit v1.2.3-18-g5258