From 3ce44fb5371d341ad8ce82c371268a6f54545470 Mon Sep 17 00:00:00 2001 From: Akhil John Peter Date: Thu, 9 Mar 2023 18:31:33 +0530 Subject: [PATCH 1/4] Added Custom Error for Duplicate Group Name --- src/authorization/exception/group.exception.ts | 12 +++++++++++- src/authorization/service/group.service.ts | 17 +++++++++++++++-- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/src/authorization/exception/group.exception.ts b/src/authorization/exception/group.exception.ts index 555cb87..72e23e4 100644 --- a/src/authorization/exception/group.exception.ts +++ b/src/authorization/exception/group.exception.ts @@ -1,4 +1,8 @@ -import { NotFoundException, PreconditionFailedException } from '@nestjs/common'; +import { + BadRequestException, + NotFoundException, + PreconditionFailedException, +} from '@nestjs/common'; export class GroupNotFoundException extends NotFoundException { constructor(groupId: string) { @@ -10,3 +14,9 @@ export class GroupDeleteNotAllowedException extends PreconditionFailedException super(`Group cannot be deleted as it is already in use`); } } + +export class GroupExistsException extends BadRequestException { + constructor(name: string) { + super(`Group with name ${name} already exists. Cannot create this group.`); + } +} diff --git a/src/authorization/service/group.service.ts b/src/authorization/service/group.service.ts index a2ccad5..63a7794 100644 --- a/src/authorization/service/group.service.ts +++ b/src/authorization/service/group.service.ts @@ -1,4 +1,4 @@ -import { Inject, Injectable } from '@nestjs/common'; +import { Inject, Injectable, BadRequestException } from '@nestjs/common'; import { DataSource } from 'typeorm'; import { SearchEntity } from '../../constants/search.entity.enum'; import { @@ -19,6 +19,7 @@ import UserGroup from '../entity/userGroup.entity'; import { GroupDeleteNotAllowedException, GroupNotFoundException, + GroupExistsException, } from '../exception/group.exception'; import { PermissionNotFoundException } from '../exception/permission.exception'; import { RoleNotFoundException } from '../exception/role.exception'; @@ -34,6 +35,7 @@ import { GroupServiceInterface } from './group.service.interface'; import { GroupCacheServiceInterface } from './groupcache.service.interface'; import SearchService from './search.service'; import { UserCacheServiceInterface } from './usercache.service.interface'; +import { DUPLICATE_ERROR_CODE } from '../../common/constants'; @Injectable() export class GroupService implements GroupServiceInterface { @@ -111,7 +113,18 @@ export class GroupService implements GroupServiceInterface { * @returns */ async createGroup(group: NewGroupInput): Promise { - return this.groupRepository.save(group); + let newGroup; + try { + newGroup = await this.groupRepository.save(group); + } catch (err) { + if (err.code === DUPLICATE_ERROR_CODE) { + err = new GroupExistsException(group.name); + } else { + err = new BadRequestException('Something Went Wrong'); + } + throw err; + } + return newGroup; } /** From d4a354d175a973e88b365e6107b2a6dbcbc89f9f Mon Sep 17 00:00:00 2001 From: Akhil John Peter Date: Fri, 10 Mar 2023 11:08:07 +0530 Subject: [PATCH 2/4] Added Constants --- src/common/constants.ts | 1 + 1 file changed, 1 insertion(+) create mode 100644 src/common/constants.ts diff --git a/src/common/constants.ts b/src/common/constants.ts new file mode 100644 index 0000000..ab4c33d --- /dev/null +++ b/src/common/constants.ts @@ -0,0 +1 @@ +export const DUPLICATE_ERROR_CODE = '23505'; From 3c07711cb7e4656f937c6b70ccfae204157831d7 Mon Sep 17 00:00:00 2001 From: Navnit Date: Fri, 10 Mar 2023 13:04:33 +0530 Subject: [PATCH 3/4] change error message --- src/authorization/authorization.module.ts | 2 ++ src/authorization/service/group.service.ts | 11 +++++++++-- test/authorization/service/group.service.test.ts | 3 +++ 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/src/authorization/authorization.module.ts b/src/authorization/authorization.module.ts index b6cc2c7..99f89df 100644 --- a/src/authorization/authorization.module.ts +++ b/src/authorization/authorization.module.ts @@ -1,6 +1,7 @@ import { Module } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { TypeOrmModule } from '@nestjs/typeorm'; +import { LoggerService } from 'src/logger/logger.service'; import { AuthenticationHelper } from '../authentication/authentication.helper'; import { RedisCacheModule } from '../cache/redis-cache/redis-cache.module'; import { RedisCacheService } from '../cache/redis-cache/redis-cache.service'; @@ -89,6 +90,7 @@ import { UserCacheServiceInterface } from './service/usercache.service.interface UserRepository, UserGroupRepository, EntityPermissionRepository, + LoggerService, { provide: EntityServiceInterface, useClass: EntityService, diff --git a/src/authorization/service/group.service.ts b/src/authorization/service/group.service.ts index 63a7794..8d8a00b 100644 --- a/src/authorization/service/group.service.ts +++ b/src/authorization/service/group.service.ts @@ -1,4 +1,8 @@ -import { Inject, Injectable, BadRequestException } from '@nestjs/common'; +import { + Inject, + Injectable, + InternalServerErrorException, +} from '@nestjs/common'; import { DataSource } from 'typeorm'; import { SearchEntity } from '../../constants/search.entity.enum'; import { @@ -36,6 +40,7 @@ import { GroupCacheServiceInterface } from './groupcache.service.interface'; import SearchService from './search.service'; import { UserCacheServiceInterface } from './usercache.service.interface'; import { DUPLICATE_ERROR_CODE } from '../../common/constants'; +import { LoggerService } from '../../logger/logger.service'; @Injectable() export class GroupService implements GroupServiceInterface { @@ -53,6 +58,7 @@ export class GroupService implements GroupServiceInterface { @Inject(UserCacheServiceInterface) private userCacheService: UserCacheServiceInterface, private searchService: SearchService, + private logger: LoggerService, ) {} /** @@ -120,7 +126,8 @@ export class GroupService implements GroupServiceInterface { if (err.code === DUPLICATE_ERROR_CODE) { err = new GroupExistsException(group.name); } else { - err = new BadRequestException('Something Went Wrong'); + this.logger.error(err); + err = new InternalServerErrorException('Something Went Wrong'); } throw err; } diff --git a/test/authorization/service/group.service.test.ts b/test/authorization/service/group.service.test.ts index de95a80..09d0dfb 100644 --- a/test/authorization/service/group.service.test.ts +++ b/test/authorization/service/group.service.test.ts @@ -2,6 +2,7 @@ import { Substitute } from '@fluffy-spoon/substitute'; import { ConfigService } from '@nestjs/config'; import { Test } from '@nestjs/testing'; import { getRepositoryToken } from '@nestjs/typeorm'; +import { LoggerService } from '../../../src/logger/logger.service'; import { DataSource, Repository, SelectQueryBuilder } from 'typeorm'; import { AuthenticationHelper } from '../../../src/authentication/authentication.helper'; import Group from '../../../src/authorization/entity/group.entity'; @@ -74,6 +75,7 @@ describe('test Group Service', () => { const roleRepository = Substitute.for>(); const userCacheService = Substitute.for(); const searchService = Substitute.for(); + const loggerService = Substitute.for(); const userQueryBuilder = Substitute.for>(); const permissionQueryBuilder = Substitute.for< SelectQueryBuilder @@ -153,6 +155,7 @@ describe('test Group Service', () => { { provide: GroupCacheServiceInterface, useValue: groupCacheService }, { provide: RedisCacheService, useValue: redisCacheService }, { provide: SearchService, useValue: searchService }, + { provide: LoggerService, useValue: loggerService }, { provide: DataSource, useValue: mockDataSource, From e5614e52dd8cc3faf09676850e890ee056305a6e Mon Sep 17 00:00:00 2001 From: Navnit Date: Fri, 10 Mar 2023 16:01:02 +0530 Subject: [PATCH 4/4] Change db error constant --- src/authorization/service/group.service.ts | 2 +- src/{common/constants.ts => constants/db.error.constants.ts} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename src/{common/constants.ts => constants/db.error.constants.ts} (100%) diff --git a/src/authorization/service/group.service.ts b/src/authorization/service/group.service.ts index 8d8a00b..bc28c19 100644 --- a/src/authorization/service/group.service.ts +++ b/src/authorization/service/group.service.ts @@ -39,7 +39,7 @@ import { GroupServiceInterface } from './group.service.interface'; import { GroupCacheServiceInterface } from './groupcache.service.interface'; import SearchService from './search.service'; import { UserCacheServiceInterface } from './usercache.service.interface'; -import { DUPLICATE_ERROR_CODE } from '../../common/constants'; +import { DUPLICATE_ERROR_CODE } from '../../constants/db.error.constants'; import { LoggerService } from '../../logger/logger.service'; @Injectable() diff --git a/src/common/constants.ts b/src/constants/db.error.constants.ts similarity index 100% rename from src/common/constants.ts rename to src/constants/db.error.constants.ts