Add base exception class MultiverseException.

Allows for localized messages to be contained in our exceptions.
This commit is contained in:
Jeremy Wood 2023-03-29 23:58:41 -04:00
parent 032a8c366d
commit 2cb134bdeb
No known key found for this signature in database
GPG Key ID: C5BAD04C77B91B4B
1 changed files with 52 additions and 0 deletions

View File

@ -0,0 +1,52 @@
package com.onarandombox.MultiverseCore.exceptions;
import com.onarandombox.MultiverseCore.commandtools.MVCommandIssuer;
import com.onarandombox.MultiverseCore.utils.message.Message;
import org.jetbrains.annotations.Nullable;
/**
* A base exception for Multiverse.
* <br/>
* {@link #getMVMessage()} provides access to a {@link Message} which can be used to provide a localized message. See
* {@link MVCommandIssuer#sendInfo(Message)}.
*/
public class MultiverseException extends Exception {
private final @Nullable Message message;
/**
* Creates a new exception with the given message and cause.
* <br/>
* If the message is not null, this exception will also contain a {@link Message} which can be accessed via
* {@link #getMVMessage()}. This message will just be the given message wrapped in a {@link Message}.
*
* @param message The message for the exception
* @param cause The cause of the exception
*/
public MultiverseException(@Nullable String message, @Nullable Throwable cause) {
this(message != null ? Message.of(message) : null, cause);
}
/**
* Creates a new exception with the given message and cause.
* <br/>
* If the message is not null, this exception will also contain a String message which can be accessed via
* {@link #getMessage()}. This message will just be the given message formatted without locale support.
*
* @param message The message for the exception
* @param cause The cause of the exception
*/
public MultiverseException(@Nullable Message message, @Nullable Throwable cause) {
super(message != null ? message.formatted() : null, cause);
this.message = message;
}
/**
* Gets the {@link Message} for this exception.
*
* @return The message, or null if none was provided
*/
public final @Nullable Message getMVMessage() {
return message;
}
}