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/api/v1/index.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) (limited to 'packages/server/src/api/v1/index.ts') diff --git a/packages/server/src/api/v1/index.ts b/packages/server/src/api/v1/index.ts index c6b9187..169ba3a 100644 --- a/packages/server/src/api/v1/index.ts +++ b/packages/server/src/api/v1/index.ts @@ -4,6 +4,7 @@ import { Route } from "../../fastify_types"; import repo from "./repo"; import { verifyRepoName } from "../util"; import { Info as APIInfo, RepositorySummary as APIRepositorySummary, Repository as APIRepository } from "shared_types"; +import { BaseError } from "../../git/error"; function setHandlers(fastify: FastifyInstance): void { fastify.setErrorHandler((err, req, reply) => { @@ -32,7 +33,7 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do return { name: repository.name.short, description: repository.description, - last_updated: (await repository.latestCommit()).date + last_updated: (await repository.masterCommit()).date }; })) }); @@ -43,13 +44,17 @@ function reposEndpoints(fastify: FastifyInstance, opts: FastifyPluginOptions, do method: "GET", url: "/repos/:repo", handler: async(req, reply) => { - const repo_verification = await verifyRepoName(opts.config.settings.base_dir, req.params.repo); - if(repo_verification.success === false && repo_verification.code) { - reply.code(repo_verification.code).send({ error: repo_verification.message }); + if(!verifyRepoName(req.params.repo)) { + reply.code(400).send({ error: "Bad request" }); return; } - const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo); + const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo).catch(err => err); + + if(repository instanceof BaseError) { + reply.code(repository.code).send({ error: repository.message }); + return; + } const data: APIRepository = { name: repository.name.short, -- cgit v1.2.3-18-g5258