From 44cb252e45650c1a4686d7d695af99b04a284084 Mon Sep 17 00:00:00 2001 From: Artur Signell Date: Fri, 26 Aug 2016 12:40:27 +0300 Subject: [PATCH] Remove dependency from ErrorMessage to old exception types Change-Id: Ifbc8d65ac5da8f6e68545ded508b28371523c5b0 --- .../vaadin/server/AbstractErrorMessage.java | 31 +++-------------- .../vaadin/server/ErrorMessageProducer.java | 34 +++++++++++++++++++ .../java/com/vaadin/v7/data/Buffered.java | 22 +++++++++++- .../java/com/vaadin/v7/data/Validator.java | 24 +++++++++++-- 4 files changed, 80 insertions(+), 31 deletions(-) create mode 100644 server/src/main/java/com/vaadin/server/ErrorMessageProducer.java diff --git a/server/src/main/java/com/vaadin/server/AbstractErrorMessage.java b/server/src/main/java/com/vaadin/server/AbstractErrorMessage.java index c3b8d0f436..0bd00bae86 100644 --- a/server/src/main/java/com/vaadin/server/AbstractErrorMessage.java +++ b/server/src/main/java/com/vaadin/server/AbstractErrorMessage.java @@ -21,9 +21,6 @@ import java.io.StringWriter; import java.util.ArrayList; import java.util.List; -import com.vaadin.v7.data.Buffered; -import com.vaadin.v7.data.Validator; - /** * Base class for component error messages. * @@ -86,7 +83,7 @@ public abstract class AbstractErrorMessage implements ErrorMessage { return level; } - protected void setErrorLevel(ErrorLevel level) { + public void setErrorLevel(ErrorLevel level) { this.level = level; } @@ -102,7 +99,7 @@ public abstract class AbstractErrorMessage implements ErrorMessage { return causes; } - protected void addCause(ErrorMessage cause) { + public void addCause(ErrorMessage cause) { causes.add(cause); } @@ -143,34 +140,14 @@ public abstract class AbstractErrorMessage implements ErrorMessage { return result; } - // TODO replace this with a helper method elsewhere? public static ErrorMessage getErrorMessageForException(Throwable t) { if (null == t) { return null; } else if (t instanceof ErrorMessage) { // legacy case for custom error messages return (ErrorMessage) t; - } else if (t instanceof Validator.InvalidValueException) { - UserError error = new UserError( - ((Validator.InvalidValueException) t).getHtmlMessage(), - ContentMode.HTML, ErrorLevel.ERROR); - for (Validator.InvalidValueException nestedException : ((Validator.InvalidValueException) t) - .getCauses()) { - error.addCause(getErrorMessageForException(nestedException)); - } - return error; - } else if (t instanceof Buffered.SourceException) { - // no message, only the causes to be painted - UserError error = new UserError(null); - // in practice, this was always ERROR in Vaadin 6 unless tweaked in - // custom exceptions implementing ErrorMessage - error.setErrorLevel(ErrorLevel.ERROR); - // causes - for (Throwable nestedException : ((Buffered.SourceException) t) - .getCauses()) { - error.addCause(getErrorMessageForException(nestedException)); - } - return error; + } else if (t instanceof ErrorMessageProducer) { + return ((ErrorMessageProducer) t).getErrorMessage(); } else { StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); diff --git a/server/src/main/java/com/vaadin/server/ErrorMessageProducer.java b/server/src/main/java/com/vaadin/server/ErrorMessageProducer.java new file mode 100644 index 0000000000..22cd4a87f4 --- /dev/null +++ b/server/src/main/java/com/vaadin/server/ErrorMessageProducer.java @@ -0,0 +1,34 @@ +/* + * Copyright 2000-2016 Vaadin Ltd. + * + * 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 com.vaadin.server; + +import java.io.Serializable; + +/** + * Interface implemented by old Vaadin 7 exception types to produce the error + * message to show in a component. + */ +@Deprecated +public interface ErrorMessageProducer extends Serializable { + + /** + * Gets the error message to show in the component. + * + * @return the error message to show + */ + ErrorMessage getErrorMessage(); + +} diff --git a/server/src/main/java/com/vaadin/v7/data/Buffered.java b/server/src/main/java/com/vaadin/v7/data/Buffered.java index cec9916b6a..025a43fbba 100644 --- a/server/src/main/java/com/vaadin/v7/data/Buffered.java +++ b/server/src/main/java/com/vaadin/v7/data/Buffered.java @@ -18,6 +18,11 @@ package com.vaadin.v7.data; import java.io.Serializable; +import com.vaadin.server.AbstractErrorMessage; +import com.vaadin.server.ErrorMessage; +import com.vaadin.server.ErrorMessage.ErrorLevel; +import com.vaadin.server.ErrorMessageProducer; +import com.vaadin.server.UserError; import com.vaadin.v7.data.Validator.InvalidValueException; /** @@ -111,7 +116,7 @@ public interface Buffered extends Serializable { */ @SuppressWarnings("serial") public class SourceException extends RuntimeException - implements Serializable { + implements Serializable, ErrorMessageProducer { /** Source class implementing the buffered interface */ private final Buffered source; @@ -173,5 +178,20 @@ public interface Buffered extends Serializable { return source; } + @Override + public ErrorMessage getErrorMessage() { + // no message, only the causes to be painted + UserError error = new UserError(null); + // in practice, this was always ERROR in Vaadin 6 unless tweaked in + // custom exceptions implementing ErrorMessage + error.setErrorLevel(ErrorLevel.ERROR); + // causes + for (Throwable nestedException : getCauses()) { + error.addCause(AbstractErrorMessage + .getErrorMessageForException(nestedException)); + } + return error; + } + } } diff --git a/server/src/main/java/com/vaadin/v7/data/Validator.java b/server/src/main/java/com/vaadin/v7/data/Validator.java index cdc0fc3412..a6ad0331ed 100644 --- a/server/src/main/java/com/vaadin/v7/data/Validator.java +++ b/server/src/main/java/com/vaadin/v7/data/Validator.java @@ -18,6 +18,12 @@ package com.vaadin.v7.data; import java.io.Serializable; +import com.vaadin.server.AbstractErrorMessage; +import com.vaadin.server.AbstractErrorMessage.ContentMode; +import com.vaadin.server.ErrorMessage; +import com.vaadin.server.ErrorMessage.ErrorLevel; +import com.vaadin.server.ErrorMessageProducer; +import com.vaadin.server.UserError; import com.vaadin.server.VaadinServlet; /** @@ -25,8 +31,8 @@ import com.vaadin.server.VaadinServlet; * valid or not. *

* Implementors of this class can be added to any - * {@link com.vaadin.v7.data.Validatable Validatable} implementor to verify - * its value. + * {@link com.vaadin.v7.data.Validatable Validatable} implementor to verify its + * value. *

*

* {@link #validate(Object)} can be used to check if a value is valid. An @@ -74,7 +80,8 @@ public interface Validator extends Serializable { * @since 3.0 */ @SuppressWarnings("serial") - public class InvalidValueException extends RuntimeException { + public class InvalidValueException extends RuntimeException + implements ErrorMessageProducer { /** * Array of one or more validation errors that are causing this @@ -159,6 +166,17 @@ public interface Validator extends Serializable { return causes; } + @Override + public ErrorMessage getErrorMessage() { + UserError error = new UserError(getHtmlMessage(), ContentMode.HTML, + ErrorLevel.ERROR); + for (Validator.InvalidValueException nestedException : getCauses()) { + error.addCause(AbstractErrorMessage + .getErrorMessageForException(nestedException)); + } + return error; + } + } /** -- 2.39.5