Преглед изворни кода

Improve error handling

Make errors more specific.
master
TheoryOfNekomata пре 2 година
родитељ
комит
04b26fd103
1 измењених фајлова са 20 додато и 6 уклоњено
  1. +20
    -6
      src/modules/git/Git.service.ts

+ 20
- 6
src/modules/git/Git.service.ts Прегледај датотеку

@@ -7,8 +7,10 @@ export interface GitService {
deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise<void>
}

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<codeCore.git.Repo> {
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<void> {
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.');
}
}

Loading…
Откажи
Сачувај