If an error occurs, the name of the affected file is shown.

This commit is contained in:
hneemann 2017-06-17 11:25:43 +02:00
parent 1afa600a87
commit 04d6748e0d
7 changed files with 70 additions and 51 deletions

View File

@ -11,6 +11,26 @@ import java.util.Set;
public class ExceptionWithOrigin extends Exception {
private Set<File> origin;
/**
* Returns the file or the files that caused the given exception.
* If no origin is found null is returned.
*
* @param e the exception
* @return the origin or null;
*/
public static String getOrigin(Throwable e) {
while (e != null) {
if (e instanceof ExceptionWithOrigin) {
String orig = ((ExceptionWithOrigin) e).getOriginStr();
if (orig != null)
return orig;
}
e = e.getCause();
}
return null;
}
/**
* Creates a new exception
*
@ -49,7 +69,7 @@ public class ExceptionWithOrigin extends Exception {
/**
* @return the origin of the error as a string
*/
public String getOriginStr() {
private String getOriginStr() {
Set<File> orig = getOrigin();
if (orig == null || orig.isEmpty())
return null;
@ -68,20 +88,10 @@ public class ExceptionWithOrigin extends Exception {
* @param origin the file which had caused the exception
*/
public void setOrigin(File origin) {
if (getOrigin() == null) {
if (this.origin == null && origin != null) {
this.origin = new HashSet<>();
this.origin.add(origin);
}
}
/**
* Sets the origin of an error
*
* @param origin a set of file which had caused the exception
*/
public void setOrigin(Set<File> origin) {
if (getOrigin() == null)
this.origin = origin;
}
}

View File

@ -83,7 +83,7 @@ public class NodeException extends ExceptionWithOrigin {
ItemConcatenation items = new ItemConcatenation(super.getMessage());
if (values != null && values.size() > 0) {
for (ObservableValue ov : values)
items.addItem(ov.getName());
items.addItem(Lang.get("msg_signal_N", ov.getName()));
}
if (nodes != null && nodes.size() > 0) {

View File

@ -124,7 +124,7 @@ public class ModelCreator implements Iterable<ModelEntry> {
}
if (isNotAIO)
entries.add(new ModelEntry(element, pins, ve, elementType.getInputDescription(ve.getElementAttributes()), isNestedCircuit));
entries.add(new ModelEntry(element, pins, ve, elementType.getInputDescription(ve.getElementAttributes()), isNestedCircuit, origin));
for (Pin p : pins)
netList.add(p);

View File

@ -11,6 +11,7 @@ import de.neemann.digital.core.element.PinDescriptions;
import de.neemann.digital.draw.elements.*;
import de.neemann.digital.lang.Lang;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
@ -26,6 +27,7 @@ public class ModelEntry {
private final Pins pins;
private final PinDescriptions inputNames;
private final boolean isNestedElement;
private final File origin; // Only used to create better error messages
private final VisualElement visualElement;
private IOState ioState;
@ -37,13 +39,15 @@ public class ModelEntry {
* @param visualElement the visual element which has created the element
* @param inputNames the pin descriptions of the inputs.
* @param isNestedElement true if this visual element is a nested included element
* @param origin Used to create better error messages
*/
public ModelEntry(Element element, Pins pins, VisualElement visualElement, PinDescriptions inputNames, boolean isNestedElement) {
public ModelEntry(Element element, Pins pins, VisualElement visualElement, PinDescriptions inputNames, boolean isNestedElement, File origin) {
this.element = element;
this.pins = pins;
this.visualElement = visualElement;
this.inputNames = inputNames;
this.isNestedElement = isNestedElement;
this.origin = origin;
}
/**
@ -53,43 +57,48 @@ public class ModelEntry {
* @throws NodeException NodeException
*/
public void applyInputs() throws PinException, NodeException {
HashMap<String, Pin> ins = pins.getInputs();
try {
HashMap<String, Pin> ins = pins.getInputs();
InverterConfig ic = visualElement.getElementAttributes().get(Keys.INVERTERCONFIG);
InverterConfig ic = visualElement.getElementAttributes().get(Keys.INVERTERCONFIG);
ObservableValues values = ObservableValues.EMPTY_LIST;
ArrayList<ObservableValue> inputs = new ArrayList<>();
for (PinDescription inputName : inputNames) {
Pin pin = ins.get(inputName.getName());
if (pin == null)
throw new PinException(Lang.get("err_pin_N0_atElement_N1_notFound", inputName, visualElement), visualElement);
ObservableValues values = ObservableValues.EMPTY_LIST;
ArrayList<ObservableValue> inputs = new ArrayList<>();
for (PinDescription inputName : inputNames) {
Pin pin = ins.get(inputName.getName());
if (pin == null)
throw new PinException(Lang.get("err_pin_N0_atElement_N1_notFound", inputName, visualElement), visualElement);
ObservableValue value = pin.getValue();
if (value == null)
throw new PinException(Lang.get("err_noValueSetFor_N0_atElement_N1", inputName, visualElement), visualElement);
ObservableValue value = pin.getValue();
if (value == null)
throw new PinException(Lang.get("err_noValueSetFor_N0_atElement_N1", inputName, visualElement), visualElement);
inputs.add(ic.invert(inputName.getName(), value));
}
ArrayList<ObservableValue> bidirect = null;
for (Pin p : pins) {
if (p.getDirection() == Pin.Direction.both) {
if (bidirect == null)
bidirect = new ArrayList<>();
final ObservableValue readerValue = p.getReaderValue();
if (readerValue == null)
throw new PinException(Lang.get("err_noValueSetFor_N0_atElement_N1", p.getName(), visualElement), visualElement);
bidirect.add(readerValue);
inputs.add(ic.invert(inputName.getName(), value));
}
}
if (bidirect != null)
inputs.addAll(bidirect);
if (inputs.size() > 0) {
values = new ObservableValues(inputs);
element.setInputs(values);
ArrayList<ObservableValue> bidirect = null;
for (Pin p : pins) {
if (p.getDirection() == Pin.Direction.both) {
if (bidirect == null)
bidirect = new ArrayList<>();
final ObservableValue readerValue = p.getReaderValue();
if (readerValue == null)
throw new PinException(Lang.get("err_noValueSetFor_N0_atElement_N1", p.getName(), visualElement), visualElement);
bidirect.add(readerValue);
}
}
if (bidirect != null)
inputs.addAll(bidirect);
if (inputs.size() > 0) {
values = new ObservableValues(inputs);
element.setInputs(values);
}
ioState = new IOState(values, element.getOutputs(), element);
} catch (PinException | NodeException e) {
e.setOrigin(origin);
throw e;
}
ioState = new IOState(values, element.getOutputs(), element);
}
/**

View File

@ -46,12 +46,10 @@ public class ErrorMessage implements Runnable {
message.append('\n');
addExceptionMessage(e);
if (e instanceof ExceptionWithOrigin) {
String orig = ((ExceptionWithOrigin) e).getOriginStr();
if (orig != null) {
if (message.length() > 0) message.append('\n');
message.append(Lang.get("msg_errInFile_N", orig));
}
String orig = ExceptionWithOrigin.getOrigin(e);
if (orig != null) {
if (message.length() > 0) message.append('\n');
message.append(Lang.get("msg_errInFile_N", orig));
}
return this;

View File

@ -916,6 +916,7 @@ Die Icons stammen aus dem Tango Desktop Project.</string>
<string name="msg_errGettingPinNames">Die Namen der Pins konnten nicht ermittelt werden.</string>
<string name="msg_errInFile_N">Aufgetreten in Datei {0}.</string>
<string name="msg_affectedComponentsAre_N">Betroffen sind: {0}.</string>
<string name="msg_signal_N">Leitung {0}</string>
<string name="ok">Ok</string>
<string name="rot_0"></string>

View File

@ -903,6 +903,7 @@ The icons are taken from the Tango Desktop Project.</string>
<string name="msg_errGettingPinNames">Could not determine the names of the pins.</string>
<string name="msg_errInFile_N">Occurred in file {0}.</string>
<string name="msg_affectedComponentsAre_N">Affected are: {0}.</string>
<string name="msg_signal_N">Signal {0}</string>
<string name="ok">Ok</string>
<string name="rot_0"></string>