Skip to content

Commit

Permalink
Merge pull request #9740 from danielb987/JmriException_multiline
Browse files Browse the repository at this point in the history
JmriException supports messages of several lines
  • Loading branch information
danielb987 committed May 3, 2021
2 parents 80839d5 + e9ddcc5 commit 6dc7851
Show file tree
Hide file tree
Showing 12 changed files with 92 additions and 104 deletions.
42 changes: 42 additions & 0 deletions java/src/jmri/JmriException.java
@@ -1,5 +1,7 @@
package jmri;

import java.util.*;

/**
* Base for JMRI-specific exceptions. No functionality, just used to confirm
* type-safety.
Expand All @@ -8,19 +10,59 @@
*/
public class JmriException extends Exception {

private final List<String> errors;

public JmriException(String s, Throwable t) {
super(s, t);
errors = null;
}

public JmriException(String s) {
super(s);
errors = null;
}

public JmriException(Throwable t) {
super(t);
errors = null;
}

public JmriException() {
errors = null;
}

public JmriException(String s, List<String> errors) {
super(s);
this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
}

public JmriException(String s, List<String> errors, Throwable t) {
super(s, t);
this.errors = Collections.unmodifiableList(new ArrayList<>(errors));
}

public List<String> getErrors() {
return errors;
}

/** {@inheritDoc} */
@Override
public String getMessage() {
if (errors != null) {
return super.getMessage() + ": " + String.join(", ", errors);
} else {
return super.getMessage();
}
}

/** {@inheritDoc} */
@Override
public String getLocalizedMessage() {
if (errors != null) {
return super.getLocalizedMessage() + ": " + String.join(", ", errors);
} else {
return super.getLocalizedMessage();
}
}

}
42 changes: 0 additions & 42 deletions java/src/jmri/JmriMultiLineException.java

This file was deleted.

Expand Up @@ -581,7 +581,7 @@ public void handleError(
Base item,
String message,
List<String> messageList,
JmriMultiLineException e,
JmriException e,
Logger log)
throws JmriException {

Expand Down
Expand Up @@ -61,10 +61,12 @@ public void setValue(double value) throws JmriException {
try {
conditionalNG.getSymbolTable().createSymbols(_localVariables);
internalSetValue(value);
} catch (JmriMultiLineException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} catch (JmriException e) {
handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
if (e.getErrors() != null) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} else {
handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
}
} catch (RuntimeException e) {
handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
}
Expand Down
Expand Up @@ -76,10 +76,12 @@ public double evaluate() throws JmriException {
try {
conditionalNG.getSymbolTable().createSymbols(_localVariables);
result = internalEvaluate();
} catch (JmriMultiLineException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluateMulti"), e.getErrors(), e, log);
} catch (JmriException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluate", e.getLocalizedMessage()), e, log);
if (e.getErrors() != null) {
handleError(this, Bundle.getMessage("ExceptionEvaluateMulti"), e.getErrors(), e, log);
} else {
handleError(this, Bundle.getMessage("ExceptionEvaluate", e.getLocalizedMessage()), e, log);
}
} catch (RuntimeException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluate", e.getLocalizedMessage()), e, log);
}
Expand Down
Expand Up @@ -43,10 +43,12 @@ public void execute() throws JmriException {
try {
conditionalNG.getSymbolTable().createSymbols(_localVariables);
((DigitalActionBean)getObject()).execute();
} catch (JmriMultiLineException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} catch (JmriException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteAction", e.getLocalizedMessage()), e, log);
if (e.getErrors() != null) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} else {
handleError(this, Bundle.getMessage("ExceptionExecuteAction", e.getLocalizedMessage()), e, log);
}
} catch (RuntimeException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteAction", e.getLocalizedMessage()), e, log);
}
Expand Down
Expand Up @@ -43,10 +43,12 @@ public void execute(boolean hasChangedToTrue, boolean hasChangedToFalse) throws
try {
conditionalNG.getSymbolTable().createSymbols(_localVariables);
((DigitalBooleanActionBean)getObject()).execute(hasChangedToTrue, hasChangedToFalse);
} catch (JmriMultiLineException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} catch (JmriException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteBooleanAction", e.getLocalizedMessage()), e, log);
if (e.getErrors() != null) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} else {
handleError(this, Bundle.getMessage("ExceptionExecuteBooleanAction", e.getLocalizedMessage()), e, log);
}
} catch (RuntimeException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteBooleanAction", e.getLocalizedMessage()), e, log);
}
Expand Down
Expand Up @@ -71,11 +71,12 @@ public boolean evaluate() throws JmriException {
try {
conditionalNG.getSymbolTable().createSymbols(_localVariables);
lastEvaluationResult = ((DigitalExpressionBean)getObject()).evaluate();
} catch (JmriMultiLineException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluateMulti"), e.getErrors(), e, log);
lastEvaluationResult = false;
} catch (JmriException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluateExpression", e.getLocalizedMessage()), e, log);
if (e.getErrors() != null) {
handleError(this, Bundle.getMessage("ExceptionEvaluateMulti"), e.getErrors(), e, log);
} else {
handleError(this, Bundle.getMessage("ExceptionEvaluateExpression", e.getLocalizedMessage()), e, log);
}
lastEvaluationResult = false;
} catch (RuntimeException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluateExpression", e.getLocalizedMessage()), e, log);
Expand Down
Expand Up @@ -48,10 +48,12 @@ public void setValue(String value) throws JmriException {
try {
conditionalNG.getSymbolTable().createSymbols(_localVariables);
((StringActionBean)getObject()).setValue(value);
} catch (JmriMultiLineException e) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} catch (JmriException e) {
handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
if (e.getErrors() != null) {
handleError(this, Bundle.getMessage("ExceptionExecuteMulti"), e.getErrors(), e, log);
} else {
handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
}
} catch (RuntimeException e) {
handleError(this, Bundle.getMessage("ExceptionSetValue", e.getLocalizedMessage()), e, log);
}
Expand Down
Expand Up @@ -56,10 +56,12 @@ public String evaluate() throws JmriException {
try {
currentConditionalNG.getSymbolTable().createSymbols(_localVariables);
result = ((StringExpressionBean)getObject()).evaluate();
} catch (JmriMultiLineException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluateMulti"), e.getErrors(), e, log);
} catch (JmriException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluate", e.getLocalizedMessage()), e, log);
if (e.getErrors() != null) {
handleError(this, Bundle.getMessage("ExceptionEvaluateMulti"), e.getErrors(), e, log);
} else {
handleError(this, Bundle.getMessage("ExceptionEvaluate", e.getLocalizedMessage()), e, log);
}
} catch (RuntimeException e) {
handleError(this, Bundle.getMessage("ExceptionEvaluate", e.getLocalizedMessage()), e, log);
}
Expand Down
14 changes: 14 additions & 0 deletions java/test/jmri/JmriExceptionTest.java
@@ -1,5 +1,8 @@
package jmri;

import java.util.ArrayList;
import java.util.List;

import jmri.util.JUnitUtil;

import org.junit.jupiter.api.*;
Expand All @@ -23,6 +26,17 @@ public void StringConstructorTest(){
Assert.assertNotNull("JmriException string constructor",new JmriException("test exception"));
}

@Test
public void ArrayConstructorTest() {
List<String> list = new ArrayList<>();
list.add("First row");
list.add("Second row");
list.add("Third row");
list.add("Forth row");
JmriException obj = new JmriException("The error", list);
Assert.assertNotNull(obj);
}

@BeforeEach
public void setUp() {
JUnitUtil.setUp();
Expand Down
39 changes: 0 additions & 39 deletions java/test/jmri/JmriMultiLineExceptionTest.java

This file was deleted.

0 comments on commit 6dc7851

Please sign in to comment.