diff options
author | HampusM <hampus@hampusmat.com> | 2021-06-21 15:40:20 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2021-06-21 15:40:20 +0200 |
commit | 647a916b251f0b1af1a59874fabb4cf8ebb245d9 (patch) | |
tree | 4e7af425b2e317140a6f04d95ae174ba7df256a9 /packages/server/src/api/git.ts | |
parent | b3a7231dbecac28ee8090f6ee84e618037f8267a (diff) |
Improved backend error handling
Diffstat (limited to 'packages/server/src/api/git.ts')
-rw-r--r-- | packages/server/src/api/git.ts | 22 |
1 files changed, 9 insertions, 13 deletions
diff --git a/packages/server/src/api/git.ts b/packages/server/src/api/git.ts index 8d532b0..cebaddc 100644 --- a/packages/server/src/api/git.ts +++ b/packages/server/src/api/git.ts @@ -1,4 +1,4 @@ -import { Commit, ConvenientHunk, ConvenientPatch, Object, Repository, Revwalk, Tag, TreeEntry } from "nodegit"; +import { Commit, ConvenientHunk, ConvenientPatch, Object, Oid, Repository, Revwalk, Tag, TreeEntry } from "nodegit"; import { FastifyReply, FastifyRequest } from "fastify"; import { join, parse } from "path"; import { readFile, readdir } from "fs"; @@ -380,7 +380,7 @@ export class GitAPI { const request_info = getRequestInfo(req); const valid_request = verifyGitRequest(request_info); - if(valid_request.success === false) { + if(valid_request.success === false && valid_request.code) { reply.header("Content-Type", request_info.content_type); reply.code(valid_request.code).send(valid_request.message); return; @@ -431,13 +431,13 @@ export class GitAPI { return { type: "blob", content: (await path_entry.getBlob()).content().toString() }; } - entries = await (await path_entry.getTree()).entries(); + entries = (await path_entry.getTree()).entries(); } catch(err) { if(err.errno === -3) { - return { error: 404 }; + return null; } - return { error: 500 }; + throw(err); } } else { @@ -466,17 +466,13 @@ export class GitAPI { }; } - async doesCommitExist(repo_name: string, commit_oid: string) { + async doesObjectExist(repo_name: string, id: string) { const full_repo_name = addRepoDirSuffix(repo_name); const repo = await Repository.openBare(`${this.base_dir}/${full_repo_name}`); - try { - await repo.getCommit(commit_oid); - return true; - } - catch { - return false; - } + return Object.lookup(repo, Oid.fromString(id), Object.TYPE.ANY) + .then(() => true) + .catch(() => false); } async doesReadmeExist(repo_name: string) { |