RuntimeException 디테일 감추기

YI Eun Gook·2020년 1월 13일
0

API를 제공할 때라거나, 등등 RuntimeException을 노출해줘야 할 때가 있다.
하지만 전부 노출하면 곤란하니까 적당~히 디테일을 감춰볼까 한다.

@RestControllerAdvice
public class MyAdvice {
  // 적당~히 Advice를 하나 만들고요
  
  @ExceptionHandler(RuntimeException.class)
  @ResponseStatus(HttpStatus.BAD_REQUEST)
  public Object handleRuntimeException(RuntimeException e) {
    RuntimeException runtimeException = new RuntimeException(e.getClass().getName());
    // 메세지는 전부 노출시키지 말고, 클래스명 정도만 노출해 줍시다.

    runtimeException.setStackTrace(new StackTraceElement[0]);
    // Throwable은 생성자에서 fillInStackTrace()을 해주기 때문에, 직접 이렇게 stackTrace를 비워줘야 합니다.
    
    return runtimeException;
  }
}
{
  "cause": null,
  "stackTrace": [],
  "message": "org.mybatis.spring.MyBatisSystemException",
  "suppressed": [],
  "localizedMessage": "org.mybatis.spring.MyBatisSystemException"
}

적당하다 그쵸!

profile
= v =?;;

0개의 댓글