Search

#018 #CI/CD-01 #Exception/Log 세팅

전제조건

✅ application.yml에서 'dev'모드로 모든 테스트 통과 ✅ RestDoc으로 Api.html 만들기 완성 ✅ AWS 계정 - 가입 후 카드등록 까지 완성한 상태 ✅ MySQL-WorkBench 8.0.37 설치가 완료된 상태
SQL
복사

사용할 Git 레파지토리 세팅

서버 Git 주소
ropa-cicd-practice
chugue
Git 헤더를 맞추고 시작하자.
git reset --hard 8775
Bash
복사

Exception Handler 설정

@Slf4j 어노테이션을 사용해서 로그를 남길 수 있는 세팅을 할 것이다. 배포 후에는 로그를 확인하는 것이 중요하기 때문이다.

Lombok 의존성 추가

build.gradle 파일 dependencies에 위의 의존성을 추가한다.
annotationProcessor 'org.projectlombok:lombok'
JavaScript
복사

ExceptionHandler 어노테이션 추가

ExceptionHandler 위에 @Slf4j 어노테이션을 달아준다.

로그 추가

ex400

@ExceptionHandler(Exception400.class) public ResponseEntity<?> ex400(Exception400 e){ log.warn("400 : " + e.getMessage()); ApiUtil<?> apiUtil = new ApiUtil<>(400, e.getMessage()); return new ResponseEntity<>(apiUtil, HttpStatus.BAD_REQUEST); }
Java
복사
설명
상태코드 400은 클라이언트의 잘못된 요청

ex401

@ExceptionHandler(Exception401.class) public ResponseEntity<?> ex401(Exception401 e, HttpServletRequest request){ log.warn("401 : " + e.getMessage()); log.warn("IP : " + request.getRemoteAddr()); // 누군지 IP 확인 log.warn("UserAgent : " + request.getHeader("UserAgent")); // 요청 장비 확인 ApiUtil<?> apiUtil = new ApiUtil<>(401, e.getMessage()); return new ResponseEntity<>(apiUtil, HttpStatus.UNAUTHORIZED); }
Java
복사
설명
상태 코드 401은 인증이 안되었을 때
비정상인 경로로 들어오는 요청을 확인
비정상인 요청의 장비 확인

ex403

@ExceptionHandler(Exception403.class) public ResponseEntity<?> ex403(Exception403 e){ log.warn("403 : " + e.getMessage()); ApiUtil<?> apiUtil = new ApiUtil<>(403, e.getMessage()); return new ResponseEntity<>(apiUtil, HttpStatus.FORBIDDEN); }
Java
복사
설명
상태코드 403은 권한이 없을 때의 에러

ex404

@ExceptionHandler(Exception404.class) public ResponseEntity<?> ex404(Exception404 e){ log.warn("404 : " + e.getMessage()); ApiUtil<?> apiUtil = new ApiUtil<>(404, e.getMessage()); return new ResponseEntity<>(apiUtil, HttpStatus.NOT_FOUND); }
Java
복사
설명
상태코드 404는 응답 리소스가 없을 때

ex500

@ExceptionHandler(Exception500.class) public ResponseEntity<?> ex500(Exception500 e){ log.warn("500 : " + e.getMessage()); ApiUtil<?> apiUtil = new ApiUtil<>(500, e.getMessage()); return new ResponseEntity<>(apiUtil, HttpStatus.INTERNAL_SERVER_ERROR); }
Java
복사
설명
상태코드 500은 내부 서버 에러

unknown Server Error

@ExceptionHandler(Exception.class) public ResponseEntity<?> unknownServerError(Exception e){ log.warn("Unknown Server Error : " + e.getMessage()); ApiUtil<?> apiUtil = new ApiUtil<>(500, e.getMessage()); return new ResponseEntity<>(apiUtil, HttpStatus.INTERNAL_SERVER_ERROR); }
Java
복사
설명
그 이외의 에러들은 일단 모두 Exception클래스로 퉁친다.