Exception 관리

[Spring] Exception 관리하기
Feb 21, 2024
Exception 관리

CustomExceptionHandler 생성

notion image
// 모든 에러 잡아내는 어노테이션 @ControllerAdvice // 응답 에러 컨트롤러 (view==파일 리턴) public class CustomExceptionHandler { @ExceptionHandler(Exception.class) public @ResponseBody String error1(Exception e){ return Script.back(e.getMessage()); } }

Exception 부분 수정

원래 return null;
notion image
Exception이 터지면서 ExceptionHandler 실행
notion image
null이 이제 return 되지 않으므로 코드 수정
notion image
Script 코드
public class Script { public static String back(String msg) { StringBuilder sb = new StringBuilder(); sb.append("<script>"); sb.append("alert('"+msg+"');"); sb.append("history.back();"); sb.append("</script>"); return sb.toString(); } public static String href(String path) { StringBuilder sb = new StringBuilder(); sb.append("<script>"); sb.append("location.href='"+path+"';"); sb.append("</script>"); return sb.toString(); } public static String href(String path, String msg) { StringBuilder sb = new StringBuilder(); sb.append("<script>"); sb.append("alert('"+msg+"');"); sb.append("location.href='"+path+"';"); sb.append("</script>"); return sb.toString(); } }
 
Share article
RSSPowered by inblog