Skip to content

Commit 3fd317f

Browse files
authored
refactor(be): refactoring exception handling logic (#239)
refactor: refactoring exception handling logic
1 parent 5f4100d commit 3fd317f

File tree

11 files changed

+245
-421
lines changed

11 files changed

+245
-421
lines changed

apps/server/src/chats/chats.repository.ts

Lines changed: 23 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,49 +2,40 @@ import { Injectable } from '@nestjs/common';
22

33
import { ChatSaveDto } from './chats.service';
44

5-
import { DatabaseException } from '@common/exceptions/resource.exception';
65
import { PrismaService } from '@prisma-alias/prisma.service';
76

87
@Injectable()
98
export class ChatsRepository {
109
constructor(private readonly prisma: PrismaService) {}
1110
async save({ sessionId, token, body }: ChatSaveDto) {
12-
try {
13-
return await this.prisma.chatting.create({
14-
data: { sessionId, createUserToken: token, body },
15-
include: {
16-
createUserTokenEntity: {
17-
select: {
18-
user: true,
19-
},
11+
return await this.prisma.chatting.create({
12+
data: { sessionId, createUserToken: token, body },
13+
include: {
14+
createUserTokenEntity: {
15+
select: {
16+
user: true,
2017
},
2118
},
22-
});
23-
} catch (error) {
24-
throw DatabaseException.create('chat');
25-
}
19+
},
20+
});
2621
}
2722
async getChatsForInfiniteScroll(sessionId: string, count: number, chatId?: number) {
28-
try {
29-
return await this.prisma.chatting.findMany({
30-
where: {
31-
sessionId,
32-
...(chatId && { chattingId: { lt: chatId } }),
33-
},
34-
include: {
35-
createUserTokenEntity: {
36-
include: {
37-
user: true,
38-
},
23+
return await this.prisma.chatting.findMany({
24+
where: {
25+
sessionId,
26+
...(chatId && { chattingId: { lt: chatId } }),
27+
},
28+
include: {
29+
createUserTokenEntity: {
30+
include: {
31+
user: true,
3932
},
4033
},
41-
orderBy: {
42-
chattingId: 'desc',
43-
},
44-
take: count,
45-
});
46-
} catch (error) {
47-
throw new Error('Error retrieving chats from database');
48-
}
34+
},
35+
orderBy: {
36+
chattingId: 'desc',
37+
},
38+
take: count,
39+
});
4940
}
5041
}
Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,7 @@
11
import { HttpException, HttpStatus } from '@nestjs/common';
22

3-
export class ResourceNotFoundException extends HttpException {
4-
constructor(resource: string) {
5-
super(`${resource}를 찾을 수 없습니다.`, HttpStatus.NOT_FOUND);
6-
}
7-
}
8-
93
export class ResourceConflictException extends HttpException {
104
constructor(message: string) {
115
super(message, HttpStatus.CONFLICT);
126
}
137
}
14-
15-
export class DatabaseException extends HttpException {
16-
constructor(operation: string) {
17-
super(`데이터베이스 ${operation} 중 오류가 발생했습니다`, HttpStatus.INTERNAL_SERVER_ERROR);
18-
}
19-
20-
static create(entity: string) {
21-
return new DatabaseException(`${entity} 생성`);
22-
}
23-
24-
static read(entity: string) {
25-
return new DatabaseException(`${entity} 조회`);
26-
}
27-
28-
static update(entity: string) {
29-
return new DatabaseException(`${entity} 수정`);
30-
}
31-
32-
static delete(entity: string) {
33-
return new DatabaseException(`${entity} 삭제`);
34-
}
35-
}

apps/server/src/common/guards/session-token-validation.guard.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,7 @@ export class SessionTokenValidationGuard implements CanActivate {
1010
private readonly sessionsAuthRepository: SessionsAuthRepository,
1111
) {}
1212

13-
async canActivate(context: ExecutionContext) {
14-
const request = context.switchToHttp().getRequest();
15-
const sessionId = request.body?.sessionId || request.query?.sessionId || request.params?.sessionId;
16-
const token = request.body?.token || request.query?.token;
17-
13+
async validateSessionToken(sessionId: string, token: string) {
1814
if (!sessionId || !token) {
1915
throw new ForbiddenException('세션 ID와 사용자 토큰이 필요합니다.');
2016
}
@@ -33,7 +29,14 @@ export class SessionTokenValidationGuard implements CanActivate {
3329
if (!userSessionToken || userSessionToken.sessionId !== sessionId) {
3430
throw new ForbiddenException('해당 세션에 접근할 권한이 없습니다.');
3531
}
32+
}
33+
34+
async canActivate(context: ExecutionContext) {
35+
const request = context.switchToHttp().getRequest();
36+
const sessionId = request.body?.sessionId || request.query?.sessionId || request.params?.sessionId;
37+
const token = request.body?.token || request.query?.token;
3638

39+
await this.validateSessionToken(sessionId, token);
3740
return true;
3841
}
3942
}

0 commit comments

Comments
 (0)