Skip to content

Commit

Permalink
Detailed error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
dima-dencep committed Dec 8, 2024
1 parent 15f12c4 commit 1480d3c
Showing 1 changed file with 14 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -76,7 +75,9 @@ public ByteBuffer write() throws IOException {
}
}
});
if(sizeSum.get() > data.sizeLimit)throw new IOException("Can't send emote, packet's size is bigger than max allowed");
if(sizeSum.get() > data.sizeLimit)throw new IOException(String.format(
"Can't send emote, packet's size (%s) is bigger than max allowed (%s)!", sizeSum.get(), data.sizeLimit
));
SongPacket songPacket = (SongPacket) subPackets.get((byte)3);
int songSize = songPacket.calculateSize(this.data) + 6;
if(songPacket.doWrite(this.data) && sizeSum.get() + songSize <= data.sizeLimit){
Expand All @@ -91,17 +92,16 @@ public ByteBuffer write() throws IOException {
buf.put(data.purpose.id);
buf.put(partCount.get());

AtomicBoolean ex = new AtomicBoolean(false);
subPackets.forEach((aByte, packet) -> {
try {
try {
for (AbstractNetworkPacket packet : this.subPackets.values()) {
writeSubPacket(buf, packet);
} catch (IOException exception) {
exception.printStackTrace();
ex.set(true);
}
});
if(ex.get())throw new IOException("Exception while writing sub-packages");
((Buffer)buf).flip(); // make it ready to read
} catch (Throwable th) {
throw new IOException("Exception while writing sub-packages", th);
} finally {
((Buffer)buf).flip(); // make it ready to read
}

return buf;
}

Expand All @@ -115,7 +115,9 @@ void writeSubPacket(ByteBuffer byteBuffer, AbstractNetworkPacket packetSender) t
int currentIndex = byteBuffer.position();
packetSender.write(byteBuffer, this.data);
if(byteBuffer.position() != currentIndex + len){
throw new IOException("Incorrect size calculator: " + packetSender.getClass());
throw new IOException(String.format("Incorrect size calculator: %s (calculated %s, real %s)",
packetSender.getClass(), len, byteBuffer.position() - currentIndex
));
}
}
}
Expand Down

0 comments on commit 1480d3c

Please sign in to comment.