3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-04 23:30:24 +01:00

Make PacketWrapper exceptions more informative :)

Dieser Commit ist enthalten in:
Myles 2016-03-22 19:27:27 +00:00
Ursprung ad53c1ed7f
Commit 4b582a7279
11 geänderte Dateien mit 97 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -9,6 +9,7 @@ import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.remapper.ValueCreator; import us.myles.ViaVersion.api.remapper.ValueCreator;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.TypeConverter; import us.myles.ViaVersion.api.type.TypeConverter;
import us.myles.ViaVersion.exception.InformativeException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
@ -84,7 +85,11 @@ public class PacketWrapper {
if (readableObjects.isEmpty()) { if (readableObjects.isEmpty()) {
Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer."); Preconditions.checkNotNull(inputBuffer, "This packet does not have an input buffer.");
// We could in the future log input read values, but honestly for things like bulk maps, mem waste D: // We could in the future log input read values, but honestly for things like bulk maps, mem waste D:
try {
return type.read(inputBuffer); return type.read(inputBuffer);
} catch (Exception e) {
throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId());
}
} else { } else {
Pair<Type, Object> read = readableObjects.poll(); Pair<Type, Object> read = readableObjects.poll();
if (read.getKey().equals(type)) { if (read.getKey().equals(type)) {
@ -152,8 +157,7 @@ public class PacketWrapper {
} }
packetValue.getKey().write(buffer, value); packetValue.getKey().write(buffer, value);
} catch (Exception e) { } catch (Exception e) {
System.out.println(getId() + " Index: " + index + " Type: " + packetValue.getKey().getTypeName()); throw new InformativeException(e).set("Index", index).set("Type", packetValue.getKey().getTypeName()).set("Packet ID", getId());
throw e;
} }
index++; index++;
} }

Datei anzeigen

@ -2,7 +2,7 @@ package us.myles.ViaVersion.api.protocol;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import us.myles.ViaVersion.CancelException; import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;

Datei anzeigen

@ -1,6 +1,7 @@
package us.myles.ViaVersion.api.remapper; package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.exception.InformativeException;
public abstract class PacketHandler implements ValueWriter { public abstract class PacketHandler implements ValueWriter {
/** /**
@ -13,6 +14,11 @@ public abstract class PacketHandler implements ValueWriter {
@Override @Override
public void write(PacketWrapper writer, Object inputValue) throws Exception { public void write(PacketWrapper writer, Object inputValue) throws Exception {
try {
handle(writer); handle(writer);
} catch (InformativeException e) {
e.addSource(this.getClass());
throw e;
}
} }
} }

Datei anzeigen

@ -3,6 +3,7 @@ package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.api.Pair;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.InformativeException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@ -84,6 +85,7 @@ public abstract class PacketRemapper {
* @throws Exception Throws if it fails to write / read to the packet. * @throws Exception Throws if it fails to write / read to the packet.
*/ */
public void remap(PacketWrapper packetWrapper) throws Exception { public void remap(PacketWrapper packetWrapper) throws Exception {
try {
// Read all the current values // Read all the current values
for (Pair<ValueReader, ValueWriter> valueRemapper : valueRemappers) { for (Pair<ValueReader, ValueWriter> valueRemapper : valueRemappers) {
Object object = valueRemapper.getKey().read(packetWrapper); Object object = valueRemapper.getKey().read(packetWrapper);
@ -91,5 +93,9 @@ public abstract class PacketRemapper {
valueRemapper.getValue().write(packetWrapper, object); valueRemapper.getValue().write(packetWrapper, object);
} }
// If we had handlers we'd put them here // If we had handlers we'd put them here
} catch (InformativeException e) {
e.addSource(this.getClass());
throw e;
}
} }
} }

Datei anzeigen

@ -1,6 +1,7 @@
package us.myles.ViaVersion.api.remapper; package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.exception.InformativeException;
public abstract class ValueCreator implements ValueWriter { public abstract class ValueCreator implements ValueWriter {
/** /**
@ -13,6 +14,11 @@ public abstract class ValueCreator implements ValueWriter {
@Override @Override
public void write(PacketWrapper writer, Object inputValue) throws Exception { public void write(PacketWrapper writer, Object inputValue) throws Exception {
try {
write(writer); write(writer);
} catch (InformativeException e) {
e.addSource(this.getClass());
throw e;
}
} }
} }

Datei anzeigen

@ -2,6 +2,7 @@ package us.myles.ViaVersion.api.remapper;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.InformativeException;
public abstract class ValueTransformer<T1, T2> implements ValueWriter<T1> { public abstract class ValueTransformer<T1, T2> implements ValueWriter<T1> {
private final Type<T2> outputType; private final Type<T2> outputType;
@ -22,6 +23,11 @@ public abstract class ValueTransformer<T1, T2> implements ValueWriter<T1> {
@Override @Override
public void write(PacketWrapper writer, T1 inputValue) throws Exception { public void write(PacketWrapper writer, T1 inputValue) throws Exception {
try {
writer.write(outputType, transform(writer, inputValue)); writer.write(outputType, transform(writer, inputValue));
} catch (InformativeException e) {
e.addSource(this.getClass());
throw e;
}
} }
} }

Datei anzeigen

@ -1,4 +1,4 @@
package us.myles.ViaVersion; package us.myles.ViaVersion.exception;
public class CancelException extends Exception { public class CancelException extends Exception {
} }

Datei anzeigen

@ -0,0 +1,50 @@
package us.myles.ViaVersion.exception;
import java.util.HashMap;
import java.util.Map;
public class InformativeException extends Exception {
Map<String, Object> info = new HashMap<>();
int sources = 0;
public InformativeException(Throwable cause) {
super(cause);
}
public InformativeException set(String key, Object value) {
info.put(key, value);
return this;
}
public InformativeException addSource(Class<?> sourceClazz) {
return set("Source #" + sources++, getSource(sourceClazz));
}
private String getSource(Class<?> sourceClazz) {
if(sourceClazz.isAnonymousClass()){
return sourceClazz.getName() + " (Anonymous)";
}else{
return sourceClazz.getName();
}
}
@Override
public String getMessage() {
StringBuilder builder = new StringBuilder();
builder.append("Please post this error to http://github.com/MylesIsCool/ViaVersion/issues\n{");
int i = 0;
for (Map.Entry<String, Object> entry : info.entrySet()) {
builder.append((i == 0 ? "" : ", ") + entry.getKey() + ": " + entry.getValue().toString());
i++;
}
builder.append("}\nActual Error: ");
return builder.toString();
}
@Override
public synchronized Throwable fillInStackTrace() {
// Don't record this stack
return this;
}
}

Datei anzeigen

@ -3,10 +3,11 @@ package us.myles.ViaVersion.handlers;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder; import io.netty.handler.codec.ByteToMessageDecoder;
import us.myles.ViaVersion.CancelException;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.exception.InformativeException;
import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;

Datei anzeigen

@ -3,10 +3,11 @@ package us.myles.ViaVersion.handlers;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import us.myles.ViaVersion.CancelException;
import us.myles.ViaVersion.api.PacketWrapper; import us.myles.ViaVersion.api.PacketWrapper;
import us.myles.ViaVersion.api.data.UserConnection; import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.type.Type; import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.exception.CancelException;
import us.myles.ViaVersion.exception.InformativeException;
import us.myles.ViaVersion.packets.Direction; import us.myles.ViaVersion.packets.Direction;
import us.myles.ViaVersion.protocols.base.ProtocolInfo; import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.util.PipelineUtil; import us.myles.ViaVersion.util.PipelineUtil;

Datei anzeigen

@ -103,7 +103,7 @@ public class SpawnPackets {
map(Type.INT, toNewDouble); // 2 - Y - Needs to be divide by 32 map(Type.INT, toNewDouble); // 2 - Y - Needs to be divide by 32
map(Type.INT, toNewDouble); // 3 - Z - Needs to be divide by 32 map(Type.INT, toNewDouble); // 3 - Z - Needs to be divide by 32
map(Type.SHORT); // 4 - Amount to spawn map(Type.INT); // 4 - Amount to spawn DONT PUSH
} }
}); });