클라이언트가 잘못된 요청을 보내오는 경우 404 에러 페이지로 보낸다.
- 잘못된 요청의 예
- 없는 스터디 페이지 조회 시도
- 없는 프로필 페이지 조회 시도
- 무작위 이벤트 조회 시도
- 허용하지 않는 요청 시도
- 이미 종료된 스터디의 모임 생성 시도
- 이미 종료된 모임에 참가 신청 시도
- 관리자 권한이 없는 스터디 수정 시도
- .......
에러 페이지 작성(error.html)
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="fragments.html :: head"></head>
<body>
<section class="jumbotron text-center">
<div class="container">
<h1>스터디올래</h1>
<p class="lead text-muted">
잘못된 요청입니다.<br/>
</p>
<p>
<a th:href="@{/}" class="btn btn-primary my-2">첫 페이지로 이동</a>
</p>
</div>
</section>
</body>
</html>
이런 에러페이지 작성으로 잘못된 요청을 처리할 수 있지만 어떤 사람이 악의적으로 잘못된 요청을 시도한다던지, 이용자들이 제일 실수하는 요청이 무엇인지 로그로 남길필요가 있을때는 따로 핸들러를 만들어주는것이 좋다.
에러 처리하는 핸들러 작성
package me.weekbelt.studyolle.modules.main;
@Slf4j
@Controller
public class ExceptionAdvice {
@ExceptionHandler
public String handleRuntimeException(@CurrentAccount Account account,
HttpServletRequest req,
RuntimeException e) {
if (account != null) {
log.info("'{}' requested '{}'", account.getNickname(), req.getRequestURI());
} else {
log.info("requested '{}'", req.getRequestURI());
}
log.error("bad request", e);
return "error";
}
}
참고: https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-JPA-%EC%9B%B9%EC%95%B1#
'스프링과 JPA 기반 웹 어플리케이션 개발 > 9부 에러 처리 및 배포 준비' 카테고리의 다른 글
85. 배포시 고려할 것 (0) | 2020.05.18 |
---|