Browse Source

Specify error responses

Use error classes to specify the error codes and body.
master
TheoryOfNekomata 2 years ago
parent
commit
3ef32e6025
1 changed files with 7 additions and 2 deletions
  1. +7
    -2
      src/modules/git/Git.service.ts

+ 7
- 2
src/modules/git/Git.service.ts View File

@@ -1,10 +1,15 @@
import {constants} from 'http2';
import * as codeCore from '@modal/code-core'; import * as codeCore from '@modal/code-core';
import {HttpError} from '../../packages/fastify-compliant-http-errors';


export interface GitService { export interface GitService {
createRepo(options: codeCore.git.CreateRepoData, user?: codeCore.common.User): Promise<codeCore.git.Repo>; createRepo(options: codeCore.git.CreateRepoData, user?: codeCore.common.User): Promise<codeCore.git.Repo>;
deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise<void> 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 GitServiceImpl implements GitService { export class GitServiceImpl implements GitService {
private readonly coreGitService: codeCore.git.GitService; private readonly coreGitService: codeCore.git.GitService;


@@ -16,13 +21,13 @@ export class GitServiceImpl implements GitService {
if (user) { if (user) {
return this.coreGitService.create(options, user); return this.coreGitService.create(options, user);
} }
throw new Error('Unauthorized');
throw new UnableToCreateRepoError('Unauthorized');
} }


async deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise<void> { async deleteRepo(repoId: codeCore.git.Repo['id'], user?: codeCore.common.User): Promise<void> {
if (user) { if (user) {
await this.coreGitService.delete(repoId, user); await this.coreGitService.delete(repoId, user);
} }
throw new Error('Unauthorized');
throw new UnableToDeleteRepoError('Unauthorized');
} }
} }

Loading…
Cancel
Save