[자문자답] 디비 중복 오류 검출 방법 변경? QueryFailedError
939
작성한 질문수 1
자문자답입니다.
강사님 코드에서는 아래와 같이 error.code 값을 비교해서 검사하는데요.
try {
await this.save(user);
} catch (error) {
if (error.code === '23505') {
throw new ConflictException('Existing username');
} else {
throw new InternalServerErrorException();
}
}
이 방법은 특정 데이터베이스(postgres?) 의존적일 가능성이 있어 보입니다.
좀더 일반적인 방법을 찾아보다가 발견한 건데요.
오류를 콘솔 로그에서 확인해보면, QueryFailedError 객체로 나옵니다.
QueryFailedError: duplicate key value violates unique constraint "UQ_78a916df40e02a9deb1c4b75edb"
at new QueryFailedError (/Users/alexlee/Develop/practices/nestjs/nestjs-board-app/src/error/QueryFailedError.ts:11:9)
at PostgresQueryRunner.<anonymous> (/Users/alexlee/Develop/practices/nestjs/nestjs-board-app/src/driver/postgres/PostgresQueryRunner.ts:228:19)
at step (/Users/alexlee/Develop/practices/nestjs/nestjs-board-app/node_modules/tslib/tslib.js:144:27)
at Object.throw (/Users/alexlee/Develop/practices/nestjs/nestjs-board-app/node_modules/tslib/tslib.js:125:57)
at rejected (/Users/alexlee/Develop/practices/nestjs/nestjs-board-app/node_modules/tslib/tslib.js:116:69)
at processTicksAndRejections (node:internal/process/task_queues:95:5) {
length: 231,
severity: 'ERROR',
code: '23505',
detail: 'Key (username)=(alexlee) already exists.',
QueryFailedError 객체 타입인지 아닌지 비교하면 어떨까요?
try {
await this.save(user);
} catch (error) {
if (error instanceof QueryFailedError) {
throw new ConflictException('Existing username');
} else {
throw new InternalServerErrorException();
}
}
제가 해본 테스트에서는 잘 검출되네요.
이 강의 보시는 다른 분들께도 도움이 되길 바랍니다.
답변 0
로거 객체 질문
0
71
1
회원가입 기능 구현 버전 변경에 따른 코드수정(해당 사항은 업데이트 예정이 없나요?)
0
88
1
파일을 찾지 못하는 오류가 계속 뜹니다
0
113
1
services와 repository 파일에서 해야하는 작업
0
138
1
커스텀 파이프에서 value의 타입이 string 이 아닐때
0
184
1
nestjs 기본 구조 설명에서 궁금한게 있습니다.
0
183
0
typeorm ^10.0.2 버전 사용시 No metadata for "BoardRepository" was found. 오류 발생 해결방법.
3
280
1
Model과 Dto의 차이점을 좀 쉽게 알수 있을까요?
0
696
1
강의 자료 관련해서 글 남깁니다.
0
486
1
"ID로 특정 게시물 가져오기" 부분이 잘못되었습니다.
0
457
1
선생님 nestjs랑 Nextjs랑 같이 연동해서 작업하는거 어떻게 생각하시나요?
0
2534
1
쌤 근데 enum 말고 type 으로 타입선언해주면안될까요?
0
628
1
pgAdmin 4에서 데이터 베이스 만들어주기 부분
0
662
1
강의 자료 관련해서 질문있습니다.
0
471
2
port 5432 failed: recived invalid response to ssl negotiation
0
776
1
repository.ts 에서 method 를 가져 오고 싶은데, 해당 메소드가 없다고 뜨네요
0
407
1
컨트롤러에선 async/await 사용하지 않아도 되는 이유
1
702
1
마지막 강의 영상에서 배포 자료는 어디서 볼 수 있나요?
0
392
1
No repository for 질문
0
531
1
특정 게시물을 찾을 때 없는 경우 에러 메세지 생성 에러
0
483
1
XML파일도 링크 공유해주시면 감사하겠습니다.
0
351
1
회원가입 기능 구현 중 오류가 났습니다.
0
502
1
NotFoundException
0
712
1
도와주세요 ㅠㅠ!!
0
1087
2





