From 7fabb02bfe5672380f3ebdfea7b3bd5c32a54149 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 22 Jul 2021 20:23:02 +0200 Subject: Improved error handling in some misc functions --- packages/server/src/git/error.ts | 6 ++++++ packages/server/src/git/misc.ts | 13 ++++++++----- packages/server/src/git/repository.ts | 6 ++++-- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/packages/server/src/git/error.ts b/packages/server/src/git/error.ts index 0429a6c..a07d349 100644 --- a/packages/server/src/git/error.ts +++ b/packages/server/src/git/error.ts @@ -42,6 +42,12 @@ export class BlobError extends BaseError { } } +export class MiscError extends BaseError { + constructor(code: number, message: string) { + super(code, "A misc error has occured: " + message); + } +} + type ErrorConstructorType = new (code: number, message: string) => T; export function createError(ErrorConstructor: ErrorConstructorType, code: number, message: string): E { diff --git a/packages/server/src/git/misc.ts b/packages/server/src/git/misc.ts index 637cb8c..bd18322 100644 --- a/packages/server/src/git/misc.ts +++ b/packages/server/src/git/misc.ts @@ -1,4 +1,5 @@ import { readFile, readdir } from "fs"; +import { createError, MiscError } from "./error"; export async function findAsync(arr: T[], callback: (t: T) => Promise): Promise { const results = await Promise.all(arr.map(callback)); @@ -6,23 +7,25 @@ export async function findAsync(arr: T[], callback: (t: T) => Promise { - return new Promise(resolve => { +export function getFile(base_dir: string, repository: string, file: string): Promise { + return new Promise((resolve, reject) => { readFile(`${base_dir}/${repository}/${file}`, (err, content) => { if(err) { - resolve(null); + reject(createError(MiscError, 500, "Failed to open repository file " + file)); return; } + resolve(content.toString().replace(/\n/gu, "")); }); }); } export function getDirectory(directory: string): Promise { - return new Promise(resolve => { + return new Promise((resolve, reject) => { readdir(directory, (err, dir_content) => { if(err) { - resolve([]); + reject(createError(MiscError, 500, "Failed to open directory " + directory)); + return; } resolve(dir_content); diff --git a/packages/server/src/git/repository.ts b/packages/server/src/git/repository.ts index c95edaa..2b14e89 100644 --- a/packages/server/src/git/repository.ts +++ b/packages/server/src/git/repository.ts @@ -7,7 +7,7 @@ import { Commit } from "./commit"; import { FastifyReply } from "fastify"; import { Tag } from "./tag"; import { Tree } from "./tree"; -import { BranchError, createError, RepositoryError } from "./error"; +import { BaseError, BranchError, createError, RepositoryError } from "./error"; import { isNodeGitReferenceBranch, isNodeGitReferenceTag, Reference } from "./reference"; function getFullRepositoryName(repo_name: string) { @@ -110,9 +110,11 @@ export class Repository { } } + const owner = await getFile(base_dir, getFullRepositoryName(repository), "owner").catch(err => err); + return new Repository(ng_repository, { description: await getFile(base_dir, getFullRepositoryName(repository), "description"), - owner: await getFile(base_dir, getFullRepositoryName(repository), "owner"), + owner: owner instanceof BaseError ? null : owner, branch: branch || "master" }); } -- cgit v1.2.3-18-g5258