Skip to content
Snippets Groups Projects
Commit a2d34d8c authored by dgelessus's avatar dgelessus
Browse files

Automatically add command name to UserErrorExceptions from commands

parent 485635ec
Branches
Tags
No related merge requests found
......@@ -23,6 +23,7 @@ import de.prob.unicode.UnicodeTranslator;
import de.prob2.jupyter.commands.BrowseCommand;
import de.prob2.jupyter.commands.CellCommand;
import de.prob2.jupyter.commands.CommandExecutionException;
import de.prob2.jupyter.commands.ConstantsCommand;
import de.prob2.jupyter.commands.ExecCommand;
import de.prob2.jupyter.commands.GroovyCommand;
......@@ -102,7 +103,11 @@ public final class ProBKernel extends BaseKernel {
if (command == null) {
throw new NoSuchCommandException(name);
}
try {
return command.run(this, name, argString, body);
} catch (final UserErrorException e) {
throw new CommandExecutionException(name, e);
}
}
private @NotNull DisplayData executeLineCommand(final @NotNull String name, final @NotNull String argString) {
......@@ -110,7 +115,11 @@ public final class ProBKernel extends BaseKernel {
if (command == null) {
throw new NoSuchCommandException(name);
}
try {
return command.run(this, name, argString);
} catch (final UserErrorException e) {
throw new CommandExecutionException(name, e);
}
}
private @NotNull DisplayData displayDataForEvalResult(final @NotNull AbstractEvalResult aer) {
......
......@@ -18,6 +18,7 @@ import de.prob.statespace.Trace;
import de.prob.statespace.Transition;
import de.prob2.jupyter.ProBKernel;
import de.prob2.jupyter.UserErrorException;
import io.github.spencerpark.jupyter.messages.DisplayData;
......@@ -57,7 +58,7 @@ public final class BrowseCommand implements LineCommand {
@Override
public @NotNull DisplayData run(final @NotNull ProBKernel kernel, final @NotNull String name, final @NotNull String argString) {
if (!argString.isEmpty()) {
throw new CommandExecutionException(name, "Unexpected argument: " + argString);
throw new UserErrorException("Unexpected argument: " + argString);
}
final Trace trace = this.animationSelector.getCurrentTrace();
......
......@@ -3,23 +3,18 @@ package de.prob2.jupyter.commands;
import de.prob2.jupyter.UserErrorException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class CommandExecutionException extends UserErrorException {
private static final long serialVersionUID = 1L;
private final @NotNull String commandName;
public CommandExecutionException(final @NotNull String commandName, final @NotNull String message, final @Nullable Throwable cause) {
super(formatMessage(commandName, message), cause);
public CommandExecutionException(final @NotNull String commandName, final @NotNull Throwable cause) {
super(formatMessage(commandName, cause.getMessage()), cause);
this.commandName = commandName;
}
public CommandExecutionException(final @NotNull String commandName, final @NotNull String message) {
this(commandName, message, null);
}
private static @NotNull String formatMessage(final @NotNull String commandName, final @NotNull String message) {
return String.format("%s: %s", commandName, message);
}
......
......@@ -6,6 +6,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import de.prob2.jupyter.UserErrorException;
import org.jetbrains.annotations.NotNull;
public final class CommandUtils {
......@@ -23,7 +25,7 @@ public final class CommandUtils {
for (final String arg : args) {
final String[] split = arg.split("=", 2);
if (split.length == 1) {
throw new CommandExecutionException(name, "Missing value for preference " + split[0]);
throw new UserErrorException("Missing value for preference " + split[0]);
}
preferences.put(split[0], split[1]);
}
......
......@@ -12,6 +12,7 @@ import de.prob.statespace.Trace;
import de.prob.statespace.Transition;
import de.prob2.jupyter.ProBKernel;
import de.prob2.jupyter.UserErrorException;
import io.github.spencerpark.jupyter.messages.DisplayData;
......@@ -50,7 +51,7 @@ public final class ExecCommand implements LineCommand {
if (opt.isPresent()) {
// Transition found, nothing else needs to be done.
if (split.length != 1) {
throw new CommandExecutionException(name, "Cannot specify a predicate when executing an operation by ID");
throw new UserErrorException("Cannot specify a predicate when executing an operation by ID");
}
op = opt.get();
} else {
......
......@@ -7,6 +7,7 @@ import java.util.TreeMap;
import com.google.inject.Inject;
import de.prob2.jupyter.ProBKernel;
import de.prob2.jupyter.UserErrorException;
import io.github.spencerpark.jupyter.messages.DisplayData;
......@@ -63,11 +64,11 @@ public final class HelpCommand implements LineCommand {
}
if (command == null) {
throw new CommandExecutionException(name, String.format("Cannot display help for unknown command \"%s\"", commandName));
throw new UserErrorException(String.format("Cannot display help for unknown command \"%s\"", commandName));
}
return new DisplayData(command.getLongHelp());
} else {
throw new CommandExecutionException(name, "Expected at most 1 argument, got " + args.size());
throw new UserErrorException("Expected at most 1 argument, got " + args.size());
}
}
}
......@@ -12,6 +12,7 @@ import de.prob.statespace.AnimationSelector;
import de.prob.statespace.Trace;
import de.prob2.jupyter.ProBKernel;
import de.prob2.jupyter.UserErrorException;
import io.github.spencerpark.jupyter.messages.DisplayData;
......@@ -43,7 +44,7 @@ public final class LoadFileCommand implements LineCommand {
public @NotNull DisplayData run(final @NotNull ProBKernel kernel, final @NotNull String name, final @NotNull String argString) {
final List<String> args = CommandUtils.splitArgs(argString);
if (args.isEmpty()) {
throw new CommandExecutionException(name, "Missing machine file name");
throw new UserErrorException("Missing machine file name");
}
final String fileName = args.get(0);
......
......@@ -13,6 +13,7 @@ import de.prob.animator.command.SetPreferenceCommand;
import de.prob.statespace.AnimationSelector;
import de.prob2.jupyter.ProBKernel;
import de.prob2.jupyter.UserErrorException;
import io.github.spencerpark.jupyter.messages.DisplayData;
......@@ -67,7 +68,7 @@ public final class PrefCommand implements LineCommand {
final List<GetPreferenceCommand> cmds = new ArrayList<>();
for (final String arg : args) {
if (arg.contains("=")) {
throw new CommandExecutionException(name, String.format("Cannot view and change preferences in the same command (attempted to assign preference %s)", arg));
throw new UserErrorException(String.format("Cannot view and change preferences in the same command (attempted to assign preference %s)", arg));
}
cmds.add(new GetPreferenceCommand(arg));
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment