Skip to content

Commit

Permalink
DefaultHandlerExceptionResolver respects custom ModelAndView
Browse files Browse the repository at this point in the history
  • Loading branch information
rstoyanchev committed Feb 14, 2023
1 parent f54e1ef commit 9a4df5a
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,9 +212,8 @@ else if (ex instanceof AsyncRequestTimeoutException) {
(AsyncRequestTimeoutException) ex, request, response, handler);
}

if (mav == null) {
return handleErrorResponse((ErrorResponse) ex, request, response, handler);
}
return (mav != null ? mav :
handleErrorResponse((ErrorResponse) ex, request, response, handler));
}

// Other, lower level exceptions
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -20,6 +20,8 @@
import java.util.Arrays;
import java.util.Collections;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -32,6 +34,7 @@
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.http.converter.HttpMessageNotWritableException;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.lang.Nullable;
import org.springframework.validation.BeanPropertyBindingResult;
import org.springframework.validation.BindException;
import org.springframework.web.HttpMediaTypeNotSupportedException;
Expand All @@ -42,6 +45,7 @@
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;
import org.springframework.web.multipart.support.MissingServletRequestPartException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.testfixture.servlet.MockHttpServletRequest;
Expand Down Expand Up @@ -232,6 +236,27 @@ public void handleAsyncRequestTimeoutException() throws Exception {
assertThat(response.getStatus()).as("Invalid status code").isEqualTo(503);
}

@Test
public void customModelAndView() {
ModelAndView expected = new ModelAndView();

HandlerExceptionResolver resolver = new DefaultHandlerExceptionResolver() {

@Override
protected ModelAndView handleHttpRequestMethodNotSupported(
HttpRequestMethodNotSupportedException ex, HttpServletRequest request,
HttpServletResponse response, @Nullable Object handler) {

return expected;
}
};

HttpRequestMethodNotSupportedException ex = new HttpRequestMethodNotSupportedException("GET");

ModelAndView actual = resolver.resolveException(request, response, null, ex);
assertThat(actual).isSameAs(expected);
}


@SuppressWarnings("unused")
public void handle(String arg) {
Expand Down

0 comments on commit 9a4df5a

Please sign in to comment.