From 647a916b251f0b1af1a59874fabb4cf8ebb245d9 Mon Sep 17 00:00:00 2001 From: HampusM Date: Mon, 21 Jun 2021 15:40:20 +0200 Subject: Improved backend error handling --- packages/server/src/api/git.ts | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) (limited to 'packages/server/src/api/git.ts') 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) { -- cgit v1.2.3-18-g5258