use of se.michaelthelin.spotify.exceptions.detailed.UnauthorizedException 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);
}
}
Aggregations