UndeclaredThrowableException?? (1) 본문

Troubleshooting

UndeclaredThrowableException?? (1)

miensoap 2024. 6. 2. 22:37
Spring AOP + ControllerAdvice 사용 중 만난 `UndeclaredThrowableException`



상황

https://github.com/codesquad-masters2024-team01/issue-tracker/tree/BE

 

GitHub - codesquad-masters2024-team01/issue-tracker: 그룹 프로젝트

그룹 프로젝트. Contribute to codesquad-masters2024-team01/issue-tracker development by creating an account on GitHub.

github.com

 

프로젝트 진행 중

 

`Authorization header` 의 JWT 토큰을 검증하고, 유저 ID 정보를 얻는 로직을 Spring AOP를 이용해,

컨트롤러 메서드에는 다음과 같이 어노테이션만 작성하도록 구현했습니다.

@Authenticate
@PostMapping
public IssueListResponse createIssue( ... @AuthenticatedUserId String userId)

 

 

custom exception으로 정의한 `AuthenticateException`

public class AuthenticateException extends RuntimeException

 

`AuthenticateException` 을 처리하는 ControllerAdvice의 메서드

@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(AuthenticateException.class)
public String handleAuthenticateException(AuthenticateException e)

 

그런데, `AuthenticateException` 이 발생할 것이라 생각했던 상황에

`UndeclaredThrowableException` 이 ControllerAdvice로 넘어와, 400 코드로 처리되는 상황이 발생했습니다.

 


원인

 

해당 예외를 던지는 `JWTAuthenticator.authenticate` 메서드에서 엉뚱한 예외를 던지고 있었습니다..

import org.apache.tomcat.websocket.AuthenticationException;

@Override
public String authenticate(HttpServletRequest request) throws AuthenticationException

해결

알맞은 예외를 던지도록 코드를 수정하니, 의도한 대로 401 코드를 응답하는 핸들러에서 처리되었습니다!

 

 

그런데.

왜 `AuthenticationException` 이 아닌 `UndeclaredThrowableException` 이 발생했을까요?

 

2024.06.02 - [Troubleshooting] - UndeclaredThrowableException?? (2)

다음글

 

 

 

 

 

'Troubleshooting' 카테고리의 다른 글

UndeclaredThrowableException?? (2)  (0) 2024.06.02
[Java] Scanner nextInt() + nextLine()  (0) 2024.01.26