use of net.robinfriedli.aiode.exceptions.CommandRuntimeException in project aiode by robinfriedli.
the class AnswerCommand method doRun.
@Override
public void doRun() {
getContext().getGuildContext().getClientQuestionEventManager().getQuestion(getContext()).ifPresentOrElse(question -> {
AbstractCommand sourceCommand = question.getSourceCommand();
String commandInput = getCommandInput();
Splitter commaSplitter = Splitter.on(",").trimResults().omitEmptyStrings();
List<String> options = commaSplitter.splitToList(commandInput);
Object option;
if (options.size() == 1) {
option = question.get(options.get(0));
} else {
Set<Object> chosenOptions = new LinkedHashSet<>();
for (String o : options) {
Object chosen = question.get(o);
if (chosen instanceof Collection) {
chosenOptions.addAll((Collection<?>) chosen);
} else {
chosenOptions.add(chosen);
}
}
option = chosenOptions;
}
try {
targetCommand = sourceCommand.fork(getContext());
targetCommand.withUserResponse(option);
question.destroy();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new CommandRuntimeException(e);
}
}, () -> {
throw new InvalidCommandException("I don't remember asking you anything " + getContext().getUser().getName());
});
}
use of net.robinfriedli.aiode.exceptions.CommandRuntimeException in project aiode by robinfriedli.
the class CommandExecutionInterceptor method performChained.
@Override
public void performChained(Command command) {
boolean completedSuccessfully = false;
boolean failedManually = false;
String errorMessage = null;
boolean unexpectedException = false;
boolean aborted = false;
try {
try {
if (command.isAborted()) {
aborted = true;
return;
}
command.doRun();
} catch (Exception e) {
if ((command.getTask() != null && command.getTask().isTerminated()) || Thread.currentThread().isInterrupted()) {
logger.warn(String.format("Suppressed '%s' because command execution was interrupted.", e));
return;
}
try {
command.onFailure();
} catch (Exception e1) {
logger.error("Exception thrown in onFailure of command, logging this error and throwing the exception that caused the command to fail.", e1);
}
throw e;
}
if (!command.isFailed()) {
command.onSuccess();
completedSuccessfully = true;
} else {
command.onFailure();
failedManually = true;
}
} catch (AmbiguousCommandException e) {
if (command instanceof AbstractCommand) {
((AbstractCommand) command).askQuestion(e.getOptions(), e.getDisplayFunc());
} else {
throw e;
}
} catch (CommandFailure e) {
throw e;
} catch (NoLoginException e) {
MessageChannel channel = command.getContext().getChannel();
User user = command.getContext().getUser();
String message = "User " + user.getName() + " is not logged in to Spotify";
messageService.sendError(message, channel);
errorMessage = message;
throw new CommandFailure(e);
} catch (UserException e) {
messageService.sendTemporary(e.buildEmbed().build(), command.getContext().getChannel());
errorMessage = e.getMessage();
throw new CommandFailure(e);
} catch (FriendlyException e) {
EmbedBuilder embedBuilder = new EmbedBuilder();
embedBuilder.setTitle("Could not load track");
if (e.getMessage() != null) {
embedBuilder.setDescription("Message returned by source: " + e.getMessage());
}
embedBuilder.setColor(Color.RED);
messageService.sendTemporary(embedBuilder.build(), command.getContext().getChannel());
throw new CommandFailure(e);
} catch (UnauthorizedException e) {
String message = "Unauthorized: " + e.getMessage();
messageService.sendException(message, command.getContext().getChannel());
logger.warn("Unauthorized Spotify API operation", e);
errorMessage = message;
unexpectedException = true;
throw new CommandFailure(e);
} catch (TooManyRequestsException e) {
String message = "Executing too many Spotify requests at the moment, please try again later.";
messageService.sendException(message, command.getContext().getChannel());
logger.warn("Executing too many Spotify requests", e);
errorMessage = message;
unexpectedException = true;
throw new CommandFailure(e);
} catch (GoogleJsonResponseException e) {
String message = e.getDetails().getMessage();
StringBuilder responseBuilder = new StringBuilder("Error occurred when requesting data from YouTube.");
if (!Strings.isNullOrEmpty(message)) {
responseBuilder.append(" Error response: ").append(message);
}
messageService.sendException(responseBuilder.toString(), command.getContext().getChannel());
logger.error("Exception during YouTube request", e);
errorMessage = message;
unexpectedException = true;
throw new CommandFailure(e);
} catch (ErrorResponseException e) {
messageService.sendException(String.format("Discord returned error (code: %d): %s", e.getErrorCode(), e.getMeaning()), command.getContext().getChannel());
logger.error("Blocking Discord request returned error", e);
throw new CommandFailure(e);
} catch (CommandRuntimeException e) {
if (e.getCause() != null) {
errorMessage = e.getCause().getClass().getSimpleName() + ": " + e.getCause().getMessage();
} else {
errorMessage = e.getClass().getSimpleName() + ": " + e.getMessage();
}
unexpectedException = true;
throw e;
} catch (Exception e) {
errorMessage = e.getClass().getSimpleName() + ": " + e.getMessage();
unexpectedException = true;
throw new CommandRuntimeException(e);
} finally {
postCommand(command, completedSuccessfully, failedManually, errorMessage, unexpectedException, aborted);
}
}
use of net.robinfriedli.aiode.exceptions.CommandRuntimeException in project aiode by robinfriedli.
the class Invoker method invoke.
/**
* Invoke a callable in a hibernate transaction. There is one Invoker per guild and this method runs synchronised,
* making sure the same guild can not run this method concurrently. This is to counteract the
* spamming of a command that uses this method, e.g. spamming the add command concurrently could evade the playlist
* size limit.
*
* @param session the target hibernate session, individual for each command execution
* @param callable tho callable to run
* @param <E> the return type
* @return the value the callable returns, often void
*/
public synchronized <E> E invoke(Session session, Callable<E> callable) {
boolean isNested = false;
if (session.getTransaction() == null || !session.getTransaction().isActive()) {
session.beginTransaction();
} else {
isNested = true;
}
if (isNested) {
try {
return callable.call();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new CommandRuntimeException(e);
}
}
E retVal;
try {
retVal = callable.call();
session.getTransaction().commit();
} catch (UserException e) {
session.getTransaction().rollback();
throw e;
} catch (Exception e) {
session.getTransaction().rollback();
throw new RuntimeException("Exception in invoked callable. Transaction rolled back.", e);
}
return retVal;
}
Aggregations