Proper error handling
This commit is contained in:
parent
f2bb432567
commit
1a268a4496
@ -6,6 +6,7 @@ import me.braydon.mc.RESTfulMC;
|
|||||||
import me.braydon.mc.common.DNSUtils;
|
import me.braydon.mc.common.DNSUtils;
|
||||||
import me.braydon.mc.common.packet.impl.PacketHandshakingInSetProtocol;
|
import me.braydon.mc.common.packet.impl.PacketHandshakingInSetProtocol;
|
||||||
import me.braydon.mc.common.packet.impl.PacketStatusInStart;
|
import me.braydon.mc.common.packet.impl.PacketStatusInStart;
|
||||||
|
import me.braydon.mc.exception.impl.BadRequestException;
|
||||||
import me.braydon.mc.exception.impl.ResourceNotFoundException;
|
import me.braydon.mc.exception.impl.ResourceNotFoundException;
|
||||||
import me.braydon.mc.model.server.JavaMinecraftServer;
|
import me.braydon.mc.model.server.JavaMinecraftServer;
|
||||||
import me.braydon.mc.model.token.JavaServerStatusToken;
|
import me.braydon.mc.model.token.JavaServerStatusToken;
|
||||||
@ -14,10 +15,7 @@ import me.braydon.mc.service.pinger.MinecraftServerPinger;
|
|||||||
import java.io.DataInputStream;
|
import java.io.DataInputStream;
|
||||||
import java.io.DataOutputStream;
|
import java.io.DataOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.net.InetAddress;
|
import java.net.*;
|
||||||
import java.net.InetSocketAddress;
|
|
||||||
import java.net.Socket;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The {@link MinecraftServerPinger} for
|
* The {@link MinecraftServerPinger} for
|
||||||
@ -35,10 +33,11 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger<Ja
|
|||||||
* @param hostname the hostname of the server
|
* @param hostname the hostname of the server
|
||||||
* @param port the port of the server
|
* @param port the port of the server
|
||||||
* @return the server that was pinged
|
* @return the server that was pinged
|
||||||
* @throws ResourceNotFoundException if the hostname could not be resolved
|
* @throws BadRequestException if the hostname is unknown
|
||||||
|
* @throws ResourceNotFoundException if the server could not be found
|
||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public JavaMinecraftServer ping(@NonNull String hostname, int port) throws ResourceNotFoundException {
|
public JavaMinecraftServer ping(@NonNull String hostname, int port) throws BadRequestException, ResourceNotFoundException {
|
||||||
InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address
|
InetAddress inetAddress = DNSUtils.resolveA(hostname); // Resolve the hostname to an IP address
|
||||||
String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address
|
String ip = inetAddress == null ? null : inetAddress.getHostAddress(); // Get the IP address
|
||||||
if (ip != null) { // Was the IP resolved?
|
if (ip != null) { // Was the IP resolved?
|
||||||
@ -68,7 +67,9 @@ public final class JavaMinecraftServerPinger implements MinecraftServerPinger<Ja
|
|||||||
}
|
}
|
||||||
} catch (IOException ex) {
|
} catch (IOException ex) {
|
||||||
if (ex instanceof UnknownHostException) {
|
if (ex instanceof UnknownHostException) {
|
||||||
throw new ResourceNotFoundException("Unknown hostname: %s".formatted(hostname));
|
throw new BadRequestException("Unknown hostname: %s".formatted(hostname));
|
||||||
|
} else if (ex instanceof ConnectException) {
|
||||||
|
throw new ResourceNotFoundException(ex);
|
||||||
}
|
}
|
||||||
log.error("An error occurred pinging %s:%s:".formatted(hostname, port), ex);
|
log.error("An error occurred pinging %s:%s:".formatted(hostname, port), ex);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user