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/app.ts | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'packages/server/src/app.ts') diff --git a/packages/server/src/app.ts b/packages/server/src/app.ts index 7bc66b2..ae220a9 100644 --- a/packages/server/src/app.ts +++ b/packages/server/src/app.ts @@ -9,6 +9,7 @@ import fastifyStatic from "fastify-static"; import { join } from "path"; import { load } from "js-yaml"; import { verifyRepoName } from "./api/util"; +import { BaseError } from "./git/error"; type Settings = { host: string, @@ -95,9 +96,9 @@ fastify.route({ handler: async(req, reply) => { reply.header("Content-Type", "application/x-git-upload-pack-advertisement"); - const repo_verification = await verifyRepoName(settings.base_dir, req.params.repo); - if(repo_verification.success === false && repo_verification.code) { - reply.code(repo_verification.code).send(repo_verification.message); + if(!verifyRepoName(req.params.repo)) { + reply.code(400).send({ error: "Bad request" }); + return; } if(!req.query.service) { @@ -126,11 +127,9 @@ fastify.route({ method: "POST", url: "/:repo([a-zA-Z0-9\\.\\-_]+)/git-upload-pack", handler: async(req, reply) => { - const repo_verification = await verifyRepoName(settings.base_dir, req.params.repo); - - if(repo_verification.success === false && repo_verification.code) { - reply.header("Content-Type", "application/x-git-upload-pack-result"); - reply.code(repo_verification.code).send(repo_verification.message); + if(!verifyRepoName(req.params.repo)) { + reply.code(400).send({ error: "Bad request" }); + return; } const repository = await Repository.open(settings.base_dir, req.params.repo); @@ -151,11 +150,22 @@ fastify.route({ method: "GET", url: "/:repo([a-zA-Z0-9\\.\\-_]+)/refs/tags/:tag", handler: async(req, reply) => { - const repository = await Repository.open(settings.base_dir, req.params.repo); - const tag = await Tag.lookup(repository, req.params.tag); + if(!verifyRepoName(req.params.repo)) { + reply.code(400).send({ error: "Bad request" }); + return; + } + + const repository: Repository | BaseError = await Repository.open(settings.base_dir, req.params.repo).catch(err => err); + + if(repository instanceof BaseError) { + reply.code(repository.code).send(repository.message); + return; + } + + const tag = await Tag.lookup(repository, req.params.tag).catch(err => err); - if(!tag) { - reply.code(404).send("Tag not found!"); + if(tag instanceof BaseError) { + reply.code(tag.code).send(tag.message); return; } -- cgit v1.2.3-18-g5258