Lack of ErrorController will not result in blowing up the application

without this change when someone has a missing ErrorController bean then TraceHandlerInterceptor blows up
with this change that problem gets fixed

fixes #512
This commit is contained in:
Marcin Grzejszczak
2017-02-13 10:45:59 +01:00
parent 11ef72369c
commit 7f6dda7179
2 changed files with 61 additions and 1 deletions

View File

@@ -23,6 +23,7 @@ import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.cloud.sleuth.Span;
import org.springframework.cloud.sleuth.TraceKeys;
@@ -188,7 +189,13 @@ public class TraceHandlerInterceptor extends HandlerInterceptorAdapter {
private ErrorController getErrorController() {
if (this.errorController == null) {
this.errorController = this.beanFactory.getBean(ErrorController.class);
try {
this.errorController = this.beanFactory.getBean(ErrorController.class);
} catch (NoSuchBeanDefinitionException e) {
if (log.isTraceEnabled()) {
log.trace("ErrorController bean not found");
}
}
}
return this.errorController;
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright 2013-2016 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.sleuth.instrument.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.BDDMockito;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
/**
* @author Marcin Grzejszczak
*/
@RunWith(MockitoJUnitRunner.class)
public class TraceHandlerInterceptorTests {
@Mock BeanFactory beanFactory;
@InjectMocks TraceHandlerInterceptor traceHandlerInterceptor;
@Test
public void should_not_blow_up_when_there_is_no_error_controller() throws Exception {
BDDMockito.given(this.beanFactory.getBean(ErrorController.class)).willThrow(new NoSuchBeanDefinitionException("errorController"));
HttpServletRequest request = new MockHttpServletRequest();
HttpServletResponse response = new MockHttpServletResponse();
Object handler = new Object();
this.traceHandlerInterceptor.afterCompletion(request, response, handler, null);
}
}