Browse Source

Remove dependency from ErrorMessage to old exception types

Change-Id: Ifbc8d65ac5da8f6e68545ded508b28371523c5b0
tags/8.0.0.alpha1
Artur Signell 7 years ago
parent
commit
44cb252e45

+ 4
- 27
server/src/main/java/com/vaadin/server/AbstractErrorMessage.java View 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);

+ 34
- 0
server/src/main/java/com/vaadin/server/ErrorMessageProducer.java View File

@@ -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();

}

+ 21
- 1
server/src/main/java/com/vaadin/v7/data/Buffered.java View 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;
}

}
}

+ 21
- 3
server/src/main/java/com/vaadin/v7/data/Validator.java View 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;
}

}

/**

Loading…
Cancel
Save