]> source.dussan.org Git - vaadin-framework.git/commitdiff
Remove dependency from ErrorMessage to old exception types
authorArtur Signell <artur@vaadin.com>
Fri, 26 Aug 2016 09:40:27 +0000 (12:40 +0300)
committerArtur Signell <artur@vaadin.com>
Fri, 26 Aug 2016 17:26:07 +0000 (17:26 +0000)
Change-Id: Ifbc8d65ac5da8f6e68545ded508b28371523c5b0

server/src/main/java/com/vaadin/server/AbstractErrorMessage.java
server/src/main/java/com/vaadin/server/ErrorMessageProducer.java [new file with mode: 0644]
server/src/main/java/com/vaadin/v7/data/Buffered.java
server/src/main/java/com/vaadin/v7/data/Validator.java

index c3b8d0f436a2f62936beadec50ffa31a63ee2db7..0bd00bae8603e60e64b7f2a822040a6cdc014d00 100644 (file)
@@ -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 (file)
index 0000000..22cd4a8
--- /dev/null
@@ -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();
+
+}
index cec9916b6a5e554ef628941221089ae8376ad7f9..025a43fbbaf44e402d15d5834f0be1349a3de9c8 100644 (file)
@@ -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;
+        }
+
     }
 }
index cdc0fc34128d4cb65219b743e31e86e073aa4d69..a6ad0331ed1e844dff82113a23d9e171b7070f9e 100644 (file)
@@ -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.
  * <p>
  * 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.
  * </p>
  * <p>
  * {@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;
+        }
+
     }
 
     /**