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.
*
return level;
}
- protected void setErrorLevel(ErrorLevel level) {
+ public void setErrorLevel(ErrorLevel level) {
this.level = level;
}
return causes;
}
- protected void addCause(ErrorMessage cause) {
+ public void addCause(ErrorMessage cause) {
causes.add(cause);
}
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);
--- /dev/null
+/*
+ * 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();
+
+}
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;
/**
*/
@SuppressWarnings("serial")
public class SourceException extends RuntimeException
- implements Serializable {
+ implements Serializable, ErrorMessageProducer {
/** Source class implementing the buffered interface */
private final Buffered source;
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;
+ }
+
}
}
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;
/**
* 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
* @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
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;
+ }
+
}
/**