From 04b26fd103b5057371eb0a8955bcd8b55aec5994 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Fri, 20 May 2022 17:49:19 +0800 Subject: [PATCH] Improve error handling Make errors more specific. --- src/modules/git/Git.service.ts | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/modules/git/Git.service.ts b/src/modules/git/Git.service.ts index 9ef657f..7be87cd 100644 --- a/src/modules/git/Git.service.ts +++ b/src/modules/git/Git.service.ts @@ -7,8 +7,10 @@ export interface GitService { deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise } -export class UnableToCreateRepoError extends HttpError(constants.HTTP_STATUS_UNAUTHORIZED, 'Unable to Create Repo') {} -export class UnableToDeleteRepoError extends HttpError(constants.HTTP_STATUS_UNAUTHORIZED, 'Unable to Delete Repo') {} +export class UnauthorizedToCreateRepoError extends HttpError(constants.HTTP_STATUS_UNAUTHORIZED, 'Unauthorized to Create Repo') {} +export class UnauthorizedToDeleteRepoError extends HttpError(constants.HTTP_STATUS_UNAUTHORIZED, 'Unauthorized to Delete Repo') {} +export class UnableToCreateRepoError extends HttpError(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, 'Unable to Create Repo') {} +export class UnableToDeleteRepoError extends HttpError(constants.HTTP_STATUS_INTERNAL_SERVER_ERROR, 'Unable to Delete Repo') {} export class GitServiceImpl implements GitService { private readonly coreGitService: codeCore.git.GitService; @@ -19,15 +21,27 @@ export class GitServiceImpl implements GitService { async createRepo(options: codeCore.git.CreateRepoData, user?: codeCore.common.User): Promise { if (user) { - return this.coreGitService.create(options, user); + let newRepo: codeCore.git.Repo; + try { + newRepo = await this.coreGitService.create(options, user); + } catch (causeRaw) { + const cause = causeRaw as Error; + throw new UnableToCreateRepoError('Something went wrong while creating the repo.', { cause, }); + } + return newRepo; } - throw new UnableToCreateRepoError('Unauthorized'); + throw new UnauthorizedToCreateRepoError('Could not create repo with insufficient authorization.'); } async deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise { if (user) { - await this.coreGitService.delete(repoId, user); + try { + await this.coreGitService.delete(repoId, user); + } catch (causeRaw) { + const cause = causeRaw as Error; + throw new UnableToDeleteRepoError('Something went wrong while deleting the repo.', { cause, }); + } } - throw new UnableToDeleteRepoError('Unauthorized'); + throw new UnauthorizedToDeleteRepoError('Could not delete repo with insufficient authorization.'); } }