aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/git/branch.ts
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2021-07-21 22:00:04 +0200
committerHampusM <hampus@hampusmat.com>2021-07-21 22:00:04 +0200
commit7b9fca8d0061cf5e5af08cba98e9d5b6dbbed8ec (patch)
tree2a75dfaaa495c90e5519012e2c8e20ab0bd7ac4e /packages/server/src/git/branch.ts
parentc0eb6635964de276b44851df67fb857ae94dc401 (diff)
Began with better backend error handling & cleaned up the backend
Diffstat (limited to 'packages/server/src/git/branch.ts')
-rw-r--r--packages/server/src/git/branch.ts21
1 files changed, 16 insertions, 5 deletions
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<CommitSummary> {
- 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<Branch | null> {
+ public static async lookup(owner: Repository, branch: string): Promise<Branch> {
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<boolean> {
+ return ng_repository.getBranch(branch)
+ .then(() => true)
+ .catch(() => false);
}
} \ No newline at end of file