From 158028881575fb3f00f96579a04ca52857afa375 Mon Sep 17 00:00:00 2001 From: Sebastien Deleuze Date: Fri, 25 Sep 2015 14:51:34 +0200 Subject: [PATCH] Improve error reporting in ScriptTemplateView This commit introduces a new ScriptRenderException in order to: - Print in the resulting error page the reason of the script failure - Not print the whole stacktrace in the logs The ScriptRenderException thrown in ScriptTemplateView#renderMergedOutputModel() is wrapped into a ServletException in order to avoid printing 2 times the messages in the logs (throwing directly a ScriptRenderException would make it wrapped in a NestedServletException that contains a getMessage() override not needed in this context) Issue: SPR-13488 --- .../view/script/ScriptRenderException.java | 45 +++++++++++++++++++ .../view/script/ScriptTemplateView.java | 3 +- 2 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptRenderException.java diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptRenderException.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptRenderException.java new file mode 100644 index 0000000000..0d4bb483e6 --- /dev/null +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptRenderException.java @@ -0,0 +1,45 @@ +/* + * Copyright 2002-2015 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.web.servlet.view.script; + +/** + * Exception thrown when an error occurs during script template rendering. + * + *

It does not print the java stacktrace in the logs, since it is not useful + * in this script context. + * + * @author Sebastien Deleuze + * @since 4.2.2 + */ +public class ScriptRenderException extends RuntimeException { + + private static final long serialVersionUID = 421565510962788082L; + + + /** + * Constructs a new script rendering exception with the specified detail message. + */ + public ScriptRenderException(String msg) { + super(msg); + } + + @Override + public synchronized Throwable fillInStackTrace() { + return this; + } + +} diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java index 8430b7e6d6..6e6ebcf8d6 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/view/script/ScriptTemplateView.java @@ -29,6 +29,7 @@ import java.util.Map; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; +import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -357,7 +358,7 @@ public class ScriptTemplateView extends AbstractUrlBasedView { response.getWriter().write(String.valueOf(html)); } catch (Exception ex) { - throw new IllegalStateException("Failed to render script template", ex); + throw new ServletException("Failed to render script template", new ScriptRenderException(ex.getMessage())); } }