aboutsummaryrefslogtreecommitdiff
path: root/packages/server/src/api/v1
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/api/v1
parentc0eb6635964de276b44851df67fb857ae94dc401 (diff)
Began with better backend error handling & cleaned up the backend
Diffstat (limited to 'packages/server/src/api/v1')
-rw-r--r--packages/server/src/api/v1/index.ts15
-rw-r--r--packages/server/src/api/v1/repo/branches.ts4
-rw-r--r--packages/server/src/api/v1/repo/index.ts42
-rw-r--r--packages/server/src/api/v1/repo/log.ts6
4 files changed, 41 insertions, 26 deletions
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 <APIRepositorySummary>{
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,
diff --git a/packages/server/src/api/v1/repo/branches.ts b/packages/server/src/api/v1/repo/branches.ts
index b280a4a..c01e858 100644
--- a/packages/server/src/api/v1/repo/branches.ts
+++ b/packages/server/src/api/v1/repo/branches.ts
@@ -8,7 +8,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/branches",
handler: async(req, reply) => {
- const branches = await (await req.repository).branches();
+ const branches = await req.repository.branches();
reply.send({
data: branches.map(branch => {
@@ -25,7 +25,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/branches/:branch",
handler: async(req, reply) => {
- const branch = await Branch.lookup(await req.repository, req.params.branch);
+ const branch = await Branch.lookup(req.repository, req.params.branch);
if(!branch) {
reply.code(404).send({ error: "Branch not found!" });
diff --git a/packages/server/src/api/v1/repo/index.ts b/packages/server/src/api/v1/repo/index.ts
index 8fc9ac1..e33484a 100644
--- a/packages/server/src/api/v1/repo/index.ts
+++ b/packages/server/src/api/v1/repo/index.ts
@@ -9,26 +9,31 @@ import branches from "./branches";
import log from "./log";
import { verifyRepoName } from "../../util";
import { Tree as APITree, Tag as APITag, TreeEntry as APITreeEntry } from "shared_types";
+import { BaseError } from "../../../git/error";
+import { Tree } from "../../../git/tree";
declare module "fastify" {
interface FastifyRequest {
- repository: Promise<Repository>,
+ repository: Repository,
}
}
function addHooks(fastify: FastifyInstance, opts: FastifyPluginOptions): void {
- fastify.addHook("preHandler", (req: CoolFastifyRequest, reply, hookDone) => {
- req.repository = Repository.open(opts.config.settings.base_dir, req.params.repo);
+ fastify.addHook("preHandler", async(req: CoolFastifyRequest, reply) => {
+ const repository = await Repository.open(opts.config.settings.base_dir, req.params.repo, req.query.branch).catch((err: BaseError) => err);
- hookDone();
+ if(repository instanceof BaseError) {
+ reply.code(repository.code).send({ error: repository.message });
+ return;
+ }
+
+ req.repository = repository;
});
fastify.addHook("onRequest", async(req: CoolFastifyRequest, 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" });
}
-
});
}
async function treeEntryMap(entry: TreeEntry) {
@@ -63,23 +68,28 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/tree",
handler: async(req, reply) => {
- const tree = await (await req.repository).tree();
+ const tree: Tree | BaseError = await req.repository.tree().catch(err => err);
+
+ if(tree instanceof BaseError) {
+ reply.code(tree.code).send({ error: tree.message });
+ return;
+ }
const tree_path = (Object.keys(req.query).length !== 0 && req.query.path) ? req.query.path : null;
let data: APITree;
if(tree_path) {
- const tree_found = await tree.find(tree_path);
+ const tree_path_entry: Tree | Blob | BaseError = await tree.find(tree_path).catch(err => err);
- if(!tree_found) {
- reply.code(404).send({ error: "Tree path not found!" });
+ if(tree_path_entry instanceof BaseError) {
+ reply.code(tree_path_entry.code).send({ error: tree_path_entry.message });
return;
}
- data = tree_found instanceof Blob
- ? { type: "blob", content: await tree_found.content() }
- : { type: "tree", content: await Promise.all(tree_found.entries().map(treeEntryMap)) };
+ data = tree_path_entry instanceof Blob
+ ? { type: "blob", content: await tree_path_entry.content() }
+ : { type: "tree", content: await Promise.all(tree_path_entry.entries().map(treeEntryMap)) };
}
else {
data = { type: "tree", content: await Promise.all(tree.entries().map(treeEntryMap)) };
@@ -93,7 +103,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/tags",
handler: async(req, reply) => {
- const tags = await (await req.repository).tags();
+ const tags = await req.repository.tags();
reply.send({
data: await Promise.all(tags.map(tagMap))
});
diff --git a/packages/server/src/api/v1/repo/log.ts b/packages/server/src/api/v1/repo/log.ts
index 8157696..fb876b8 100644
--- a/packages/server/src/api/v1/repo/log.ts
+++ b/packages/server/src/api/v1/repo/log.ts
@@ -37,7 +37,7 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/log",
handler: async(req, reply) => {
- const commits = (await (await req.repository).commits());
+ const commits = await req.repository.commits();
reply.send({
data: await Promise.all(commits.map(commitMap))
@@ -49,12 +49,12 @@ export default function(fastify: FastifyInstance, opts: FastifyPluginOptions, do
method: "GET",
url: "/log/:commit",
handler: async(req, reply) => {
- const commit_verification = await verifySHA(await req.repository, req.params.commit);
+ const commit_verification = await verifySHA(req.repository, req.params.commit);
if(commit_verification.success === false && commit_verification.code) {
reply.code(commit_verification.code).send({ error: commit_verification.message });
}
- const commit = await Commit.lookup(await req.repository, req.params.commit);
+ const commit = await Commit.lookup(req.repository, req.params.commit);
const stats = await commit.stats();