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

This commit is contained in:
hneemann 2017-06-16 18:04:37 +02:00
parent 44534b84a6
commit 81eac8aedd
8 changed files with 202 additions and 115 deletions

View File

@ -2,6 +2,7 @@ Release Notes
planned as v0.13
- In case of oscillations almost all effected components are shown.
- If an error occurs, the name of the affected file is shown.
v0.12.1, released on 05. Jun 2016
- added a fuse to simulate a PROM or PAL.

View File

@ -0,0 +1,56 @@
package de.neemann.digital.core;
import java.io.File;
/**
* A exception which has a file as an origin
* Created by hneemann on 16.06.17.
*/
public class ExceptionWithOrigin extends Exception {
private File origin;
/**
* Creates a new exception
*
* @param message message
*/
public ExceptionWithOrigin(String message) {
super(message);
}
/**
* Creates a new exception
*
* @param message message
* @param cause the cause
*/
public ExceptionWithOrigin(String message, Throwable cause) {
super(message, cause);
}
/**
* Creates a new exception
*
* @param cause the cause
*/
public ExceptionWithOrigin(Throwable cause) {
super(cause);
}
/**
* @return the origin of the error
*/
public File getOrigin() {
return origin;
}
/**
* Sets the origin of an error
*
* @param origin the file which causes the exception
*/
public void setOrigin(File origin) {
if (getOrigin() == null)
this.origin = origin;
}
}

View File

@ -8,7 +8,6 @@ import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
/**
* This exception is thrown if there was a problem creating or running the model.
@ -17,7 +16,7 @@ import java.util.HashSet;
*
* @author hneemann
*/
public class NodeException extends Exception {
public class NodeException extends ExceptionWithOrigin {
private final ArrayList<Node> nodes;
private final ImmutableList<ObservableValue> values;
private final int input;
@ -88,11 +87,7 @@ public class NodeException extends Exception {
}
if (nodes != null && nodes.size() > 0) {
HashSet<File> origins = new HashSet<>();
for (Node node : nodes) {
if (node != null && node.getOrigin() != null && node.getOrigin().length() > 0)
origins.add(node.getOrigin());
if (node != null)
try { // pick the nodes description if available
final Field field = node.getClass().getField("DESCRIPTION");
@ -111,8 +106,6 @@ public class NodeException extends Exception {
// ignore an error accessing the ElementTypeDescription
}
}
for (File o : origins)
items.addItem(o.getName());
}
return items.toString();
@ -152,6 +145,17 @@ public class NodeException extends Exception {
sb.append(")");
return sb.toString();
}
}
@Override
public File getOrigin() {
File o = super.getOrigin();
if (o != null)
return o;
for (Node n : nodes)
if (n.getOrigin() != null)
return n.getOrigin();
return null;
}
}

View File

@ -1,5 +1,6 @@
package de.neemann.digital.draw.elements;
import de.neemann.digital.core.ExceptionWithOrigin;
import de.neemann.digital.draw.model.Net;
/**
@ -7,7 +8,7 @@ import de.neemann.digital.draw.model.Net;
*
* @author hneemann
*/
public class PinException extends Exception {
public class PinException extends ExceptionWithOrigin {
private VisualElement element;
private Net net;

View File

@ -32,6 +32,7 @@ public class ModelCreator implements Iterable<ModelEntry> {
private final NetList netList;
private final ArrayList<ModelEntry> entries;
private final HashMap<String, Pin> ioMap;
private final File origin;
/**
* Creates the ModelDescription.
@ -69,7 +70,7 @@ public class ModelCreator implements Iterable<ModelEntry> {
* @param circuit the circuit to use
* @param library the library to use
* @param isNestedCircuit if true the model is created for use as nested element
* @param fileName only used for better messages in exceptions
* @param origin only used for better messages in exceptions
* @param netList the NetList of the model. If known it is not necessary to create it.
* @param subName name of the circuit, used to name unique elements
* @param depth recursion depth, used to detect a circuit which contains itself
@ -77,7 +78,8 @@ public class ModelCreator implements Iterable<ModelEntry> {
* @throws NodeException NodeException
* @throws ElementNotFoundException ElementNotFoundException
*/
public ModelCreator(Circuit circuit, ElementLibrary library, boolean isNestedCircuit, File fileName, NetList netList, String subName, int depth) throws PinException, NodeException, ElementNotFoundException {
public ModelCreator(Circuit circuit, ElementLibrary library, boolean isNestedCircuit, File origin, NetList netList, String subName, int depth) throws PinException, NodeException, ElementNotFoundException {
this.origin = origin;
this.circuit = circuit;
this.netList = netList;
entries = new ArrayList<>();
@ -86,6 +88,7 @@ public class ModelCreator implements Iterable<ModelEntry> {
else
ioMap = null;
try {
for (VisualElement ve : circuit.getElements()) {
Pins pins = ve.getPins();
ElementTypeDescription elementType = library.getElementType(ve.getElementName());
@ -100,7 +103,7 @@ public class ModelCreator implements Iterable<ModelEntry> {
// sets the nodes origin to create better error messages
if (element instanceof Node)
((Node) element).setOrigin(fileName);
((Node) element).setOrigin(origin);
// if handled as nested element, don't put pins in EntryList, but put the pins in a
// separate map to connect it with the parent!
@ -109,11 +112,11 @@ public class ModelCreator implements Iterable<ModelEntry> {
if (elementType == In.DESCRIPTION || elementType == Out.DESCRIPTION || elementType == Clock.DESCRIPTION) {
String label = ve.getElementAttributes().getLabel();
if (label == null || label.length() == 0)
throw new PinException(Lang.get("err_pinWithoutName", fileName));
throw new PinException(Lang.get("err_pinWithoutName", origin));
if (pins.size() != 1)
throw new PinException(Lang.get("err_N_isNotInputOrOutput", label, fileName));
throw new PinException(Lang.get("err_N_isNotInputOrOutput", label, origin));
if (ioMap.containsKey(label))
throw new PinException(Lang.get("err_duplicatePinLabel", label, fileName));
throw new PinException(Lang.get("err_duplicatePinLabel", label, origin));
ioMap.put(label, pins.get(0));
isNotAIO = false;
@ -188,6 +191,10 @@ public class ModelCreator implements Iterable<ModelEntry> {
entries.addAll(md.entries);
netList.add(md.netList);
}
} catch (PinException | NodeException e) {
e.setOrigin(origin);
throw e;
}
}
private String combineNames(String s1, String s2) {
@ -232,7 +239,7 @@ public class ModelCreator implements Iterable<ModelEntry> {
* @throws NodeException NodeException
*/
public Model createModel(boolean attachWires) throws PinException, NodeException {
try {
Model m = new Model();
for (Net n : netList)
@ -250,6 +257,10 @@ public class ModelCreator implements Iterable<ModelEntry> {
}
return m;
} catch (PinException | NodeException e) {
e.setOrigin(origin);
throw e;
}
}
/**

View File

@ -1,9 +1,11 @@
package de.neemann.gui;
import de.neemann.digital.core.ExceptionWithOrigin;
import de.neemann.digital.lang.Lang;
import javax.swing.*;
import java.awt.*;
import java.io.File;
/**
* Used to show error messages.
@ -44,6 +46,16 @@ public class ErrorMessage implements Runnable {
if (message.length() > 0)
message.append('\n');
addExceptionMessage(e);
if (e instanceof ExceptionWithOrigin) {
File o = ((ExceptionWithOrigin) e).getOrigin();
if (o!=null) {
if (message.length() > 0)
message.append('\n');
message.append(Lang.get("msg_errInFile_N", o.getName()));
}
}
return this;
}

View File

@ -914,6 +914,7 @@ Die Icons stammen aus dem Tango Desktop Project.</string>
<string name="msg_inputsToInvert">Zu invertierende Eingänge</string>
<string name="msg_none">keine</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="ok">Ok</string>
<string name="rot_0"></string>

View File

@ -901,6 +901,7 @@ The icons are taken from the Tango Desktop Project.</string>
<string name="msg_inputsToInvert">Inputs to invert</string>
<string name="msg_none">none</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="ok">Ok</string>
<string name="rot_0"></string>