Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[MVC 구현 - 3단계] 바론(이소민) 미션 제출합니다. #560

Merged
merged 8 commits into from
Sep 27, 2023
Prev Previous commit
Next Next commit
feat: DispatcherServlet - Handler 없을 때 에러 핸들링 추가
somsom13 committed Sep 25, 2023
commit 725e03d5f79151677d8bfe8f4c8479f86aa3bce7
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.techcourse.controller;

import com.techcourse.domain.User;
import com.techcourse.exception.UserNotFoundException;
import com.techcourse.repository.InMemoryUserRepository;
import context.org.springframework.stereotype.Controller;
import jakarta.servlet.http.HttpServletRequest;
@@ -24,7 +25,7 @@ public ModelAndView show(HttpServletRequest request, HttpServletResponse respons

final ModelAndView modelAndView = new ModelAndView(new JsonView());
final User user = InMemoryUserRepository.findByAccount(account)
.orElseThrow();
.orElseThrow(UserNotFoundException::new);

modelAndView.addObject("user", user);
return modelAndView;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.techcourse.exception;

public class UserNotFoundException extends RuntimeException {

public UserNotFoundException() {
super("존재하지 않는 사용자입니다.");
}
}
Original file line number Diff line number Diff line change
@@ -4,10 +4,12 @@
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import webmvc.org.springframework.web.servlet.mvc.adapter.HandlerAdapter;
import webmvc.org.springframework.web.servlet.mvc.adapter.HandlerAdapters;
import webmvc.org.springframework.web.servlet.mvc.exception.HandleException;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerMappings;

public class DispatcherServlet extends HttpServlet {
@@ -30,14 +32,17 @@ public void init() {

@Override
protected void service(final HttpServletRequest request, final HttpServletResponse response)
throws ServletException {
throws ServletException, IOException {
log.debug("Method : {}, Request URI : {}", request.getMethod(), request.getRequestURI());

try {
final Object handler = handlerMappings.getHandler(request);
final HandlerAdapter handlerAdapter = handlerAdapters.getHandlerAdapter(handler);
final ModelAndView modelAndView = handlerAdapter.handle(request, response, handler);
render(modelAndView, request, response);
} catch (HandleException e) {
log.error("HandleException: {}", e.getMessage(), e);
response.sendError(HttpServletResponse.SC_NOT_FOUND);
} catch (Exception e) {
log.error("Exception : {}", e.getMessage(), e);
throw new ServletException(e.getMessage());
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package webmvc.org.springframework.web.servlet.mvc.adapter;

import java.util.List;
import webmvc.org.springframework.web.servlet.mvc.exception.HandlerAdapterException;

public class HandlerAdapters {

@@ -16,6 +17,6 @@ public HandlerAdapter getHandlerAdapter(final Object handler) {
return adapters.stream()
.filter(adapter -> adapter.supports(handler))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("적합한 핸들러 어댑터가 없습니다."));
.orElseThrow(HandlerAdapterException.NotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package webmvc.org.springframework.web.servlet.mvc.exception;

public class HandleException extends RuntimeException {

public HandleException(final String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package webmvc.org.springframework.web.servlet.mvc.exception;

public class HandlerAdapterException extends HandleException {

private HandlerAdapterException(final String message) {
super(message);
}

public static class NotFoundException extends HandlerAdapterException {

public NotFoundException() {
super("요청을 처리할 수 있는 HandlerAdapter 가 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package webmvc.org.springframework.web.servlet.mvc.exception;

public class HandlerExecutionException extends RuntimeException {
public class HandlerExecutionException extends HandleException {

private HandlerExecutionException(final String message) {
super(message);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package webmvc.org.springframework.web.servlet.mvc.exception;

public class HandlerMappingException extends HandleException {

private HandlerMappingException(final String message) {
super(message);
}

public static class NotFoundException extends HandlerMappingException {

public NotFoundException() {
super("요청을 처리할 수 있는 Handler 가 없습니다.");
}
}
}
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@
import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Objects;
import webmvc.org.springframework.web.servlet.mvc.exception.HandlerMappingException;

public class HandlerMappings {

@@ -25,6 +26,6 @@ public Object getHandler(final HttpServletRequest request) {
.map(mapping -> mapping.getHandler(request))
.filter(Objects::nonNull)
.findFirst()
.orElseThrow(() -> new IllegalArgumentException("매칭되는 핸들러가 없습니다."));
.orElseThrow(HandlerMappingException.NotFoundException::new);
}
}
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import webmvc.org.springframework.web.servlet.mvc.exception.HandlerAdapterException;
import webmvc.org.springframework.web.servlet.mvc.handlermapping.HandlerExecution;

class HandlerAdaptersTest {
@@ -33,7 +34,6 @@ void getHandlerAdapter_nothingSupported() throws Exception {

// when, then
assertThatThrownBy(() -> handlerAdapters.getHandlerAdapter(handler))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("적합한 핸들러 어댑터가 없습니다.");
.isInstanceOf(HandlerAdapterException.NotFoundException.class);
}
}
Original file line number Diff line number Diff line change
@@ -8,6 +8,7 @@
import jakarta.servlet.http.HttpServletRequest;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import webmvc.org.springframework.web.servlet.mvc.exception.HandlerMappingException;

class HandlerMappingsTest {

@@ -42,7 +43,6 @@ void getHandler_nothingMatched() {

// when, then
assertThatThrownBy(() -> handlerMappings.getHandler(request))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("매칭되는 핸들러가 없습니다.");
.isInstanceOf(HandlerMappingException.NotFoundException.class);
}
}