-
필터에서 발생하는 예외 처리하기프레임워크/Spring Boot 2024. 5. 7. 23:45
Http 통신 중에 발생하는 예외를 처리하던 중 만난 이슈를 적어봅니다.
필터에서 발생하는 예외 처리에 관한 내용입니다.
1. 예외가 발생하는 상황
인증서버에서 유저 서버로부터 유저 정보를 가져오는 코드의 일부입니다.
3개의 catch 문을 가지고 있는데 각각 매칭되는 유저가 없는 경우, 유저 서버의 동작에 문제 있을 때 그리고 그 외의 모든 문제의 경우를 나눠서 처리하고 있습니다.
문제의 발생은 이렇습니다.
유저 서버를 실행하지 않은 상태로 ResourceAccessException을 발생시키고 적절한 처리를 하려고 ControlleAdvice를 정의해주었습니다.
하지만 advice에 걸리지 않고 여전히 프론트에서 에러 메세지를 바로 보는 상황이 발생합니다.
왜 예외를 못잡는가 고민하던 중 ...
컨트롤러 어드바이스는 이미 이름에서 컨트롤러에서 발생하는 예외를 처리한다라는 것을 떠올렸습니다.
지금 작업의 범위는 컨트롤러 보다 앞단에 있는 필터에서 발생하기 때문에 잡지못하는 것이었습니다.
2. 예외 필터 추가
기존의 필터 체인의 가장 앞단에 예외 필터를 추가해주었습니다.
ControllerAdivce 처럼 여러가지 예외 경우를 적절한 핸들러로 처리할 수 있습니다.
SecurityConfig 파일에 가서 적절한 위치에 필터를 추가해줍니다.
감사합니다.
'프레임워크 > Spring Boot' 카테고리의 다른 글
Kotlin & Spring 으로 Entity 생성하기 (0) 2024.07.01 ServletInputStream은 1번만 읽을 수 있다 (0) 2024.05.08 Spring Security 다이어그램으로 flow 정리하기 (0) 2024.05.07 SecurityFilterChain을 여러개 쓰고자 한다면 securityMatcher! (0) 2024.05.03 RestTemplate는 응답 객체를 LinkedHashMap으로 파싱한다 (0) 2024.05.02