Skip to content

Commit 7134f60

Browse files
authored
[refactor] add error message to all new Error() calls (#15609)
It's treated a bad practices to create exceptions without error message. Motivation: Even if it seems that the error can never happen, it still can - for example, when someone adds an enum value and forgets to update all switches. Modification: Replaced all calls of `new Error()` by `new Error("Unexpected something: " + value)`.
1 parent f7f5b48 commit 7134f60

File tree

67 files changed

+146
-134
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+146
-134
lines changed

buffer/src/main/java/io/netty/buffer/AbstractReferenceCountedByteBuf.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import io.netty.util.internal.AtomicReferenceCountUpdater;
2424
import io.netty.util.internal.PlatformDependent;
2525
import io.netty.util.internal.ReferenceCountUpdater;
26+
import io.netty.util.internal.ReferenceCountUpdater.UpdaterType;
2627
import io.netty.util.internal.UnsafeReferenceCountUpdater;
2728
import io.netty.util.internal.VarHandleReferenceCountUpdater;
2829

@@ -39,7 +40,8 @@ public abstract class AbstractReferenceCountedByteBuf extends AbstractByteBuf {
3940
private static final ReferenceCountUpdater<AbstractReferenceCountedByteBuf> updater;
4041

4142
static {
42-
switch (ReferenceCountUpdater.updaterTypeOf(AbstractReferenceCountedByteBuf.class, "refCnt")) {
43+
UpdaterType updaterType = ReferenceCountUpdater.updaterTypeOf(AbstractReferenceCountedByteBuf.class, "refCnt");
44+
switch (updaterType) {
4345
case Atomic:
4446
AIF_UPDATER = newUpdater(AbstractReferenceCountedByteBuf.class, "refCnt");
4547
REFCNT_FIELD_OFFSET = -1;
@@ -75,7 +77,7 @@ protected VarHandle varHandle() {
7577
};
7678
break;
7779
default:
78-
throw new Error("Unknown updater type for AbstractReferenceCountedByteBuf");
80+
throw new Error("Unexpected updater type for AbstractReferenceCountedByteBuf: " + updaterType);
7981
}
8082
}
8183

buffer/src/main/java/io/netty/buffer/AdaptivePoolingAllocator.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1119,7 +1119,8 @@ private static class Chunk implements ReferenceCounted, ChunkInfo {
11191119
private static final ReferenceCountUpdater<Chunk> updater;
11201120

11211121
static {
1122-
switch (ReferenceCountUpdater.updaterTypeOf(Chunk.class, "refCnt")) {
1122+
ReferenceCountUpdater.UpdaterType updaterType = ReferenceCountUpdater.updaterTypeOf(Chunk.class, "refCnt");
1123+
switch (updaterType) {
11231124
case Atomic:
11241125
AIF_UPDATER = newUpdater(Chunk.class, "refCnt");
11251126
REFCNT_FIELD_OFFSET = -1;
@@ -1155,7 +1156,7 @@ protected VarHandle varHandle() {
11551156
};
11561157
break;
11571158
default:
1158-
throw new Error("Unknown updater type for Chunk");
1159+
throw new Error("Unexpected updater type for Chunk: " + updaterType);
11591160
}
11601161
}
11611162

buffer/src/main/java/io/netty/buffer/PoolArena.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ void freeChunk(PoolChunk<T> chunk, long handle, int normCapacity, SizeClass size
272272
++deallocationsSmall;
273273
break;
274274
default:
275-
throw new Error();
275+
throw new Error("Unexpected size class: " + sizeClass);
276276
}
277277
}
278278
destroyChunk = !chunk.parent.free(chunk, handle, normCapacity, nioBuffer);

buffer/src/main/java/io/netty/buffer/PoolThreadCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ private MemoryRegionCache<?> cache(PoolArena<?> area, int sizeIdx, SizeClass siz
193193
case Small:
194194
return cacheForSmall(area, sizeIdx);
195195
default:
196-
throw new Error();
196+
throw new Error("Unexpected size class: " + sizeClass);
197197
}
198198
}
199199

buffer/src/test/java/io/netty/buffer/ByteBufDerivationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,8 @@ public void testMixture() throws Exception {
175175
Random rnd = new Random();
176176
for (int i = 0; i < buf.capacity(); i ++) {
177177
ByteBuf newDerived;
178-
switch (rnd.nextInt(4)) {
178+
int randomNumber = rnd.nextInt(4);
179+
switch (randomNumber) {
179180
case 0:
180181
newDerived = derived.slice(1, derived.capacity() - 1);
181182
break;
@@ -190,7 +191,7 @@ public void testMixture() throws Exception {
190191
newDerived = Unpooled.unmodifiableBuffer(derived);
191192
break;
192193
default:
193-
throw new Error();
194+
throw new Error("Unexpected random number: " + randomNumber);
194195
}
195196

196197
assertThat(nestLevel(newDerived)).isLessThanOrEqualTo(3);

codec-base/src/main/java/io/netty/handler/codec/AsciiHeadersEncoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void encode(Entry<CharSequence, CharSequence> entry) {
9090
buf.setByte(offset ++, ' ');
9191
break;
9292
default:
93-
throw new Error();
93+
throw new Error("Unexpected separator type: " + separatorType);
9494
}
9595

9696
writeAscii(buf, offset, value);
@@ -105,7 +105,7 @@ public void encode(Entry<CharSequence, CharSequence> entry) {
105105
buf.setByte(offset ++, '\n');
106106
break;
107107
default:
108-
throw new Error();
108+
throw new Error("Unexpected newline type: " + newlineType);
109109
}
110110

111111
buf.writerIndex(offset);

codec-base/src/main/java/io/netty/handler/codec/LengthFieldPrepender.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ protected void encode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out)
195195
out.add(ctx.alloc().buffer(8).order(byteOrder).writeLong(length));
196196
break;
197197
default:
198-
throw new Error("should not reach here");
198+
throw new Error("Unexpected length field length: " + lengthFieldLength);
199199
}
200200
out.add(msg.retain());
201201
}

codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicSslContext.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ private static int boringSSLVerifyModeForServer(ClientAuth mode) {
323323
case OPTIONAL:
324324
return BoringSSL.SSL_VERIFY_PEER;
325325
default:
326-
throw new Error(mode.toString());
326+
throw new Error("Unexpected mode: " + mode);
327327
}
328328
}
329329

codec-classes-quic/src/main/java/io/netty/handler/codec/quic/QuicheQuicStreamChannel.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -980,13 +980,14 @@ void recv() {
980980
while (!finReceived && continueReading) {
981981
byteBuf = allocHandle.allocate(allocator);
982982
allocHandle.attemptedBytesRead(byteBuf.writableBytes());
983-
switch (parent.streamRecv(streamId(), byteBuf)) {
983+
QuicheQuicChannel.StreamRecvResult result = parent.streamRecv(streamId(), byteBuf);
984+
switch (result) {
984985
case DONE:
985986
// Nothing left to read;
986987
readable = false;
987988
break;
988989
case FIN:
989-
// If we received a FIN we also should mark the channel as non readable as
990+
// If we received a FIN we also should mark the channel as non-readable as
990991
// there is nothing left to read really.
991992
readable = false;
992993
finReceived = true;
@@ -995,7 +996,7 @@ void recv() {
995996
case OK:
996997
break;
997998
default:
998-
throw new Error();
999+
throw new Error("Unexpected StreamRecvResult: " + result);
9991000
}
10001001
allocHandle.lastBytesRead(byteBuf.readableBytes());
10011002
if (allocHandle.lastBytesRead() <= 0) {

codec-compression/src/main/java/io/netty/handler/codec/compression/ZlibUtil.java

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,43 +41,32 @@ static CompressionException deflaterException(Deflater z, String message, int re
4141
}
4242

4343
static JZlib.WrapperType convertWrapperType(ZlibWrapper wrapper) {
44-
JZlib.WrapperType convertedWrapperType;
4544
switch (wrapper) {
4645
case NONE:
47-
convertedWrapperType = JZlib.W_NONE;
48-
break;
46+
return JZlib.W_NONE;
4947
case ZLIB:
50-
convertedWrapperType = JZlib.W_ZLIB;
51-
break;
48+
return JZlib.W_ZLIB;
5249
case GZIP:
53-
convertedWrapperType = JZlib.W_GZIP;
54-
break;
50+
return JZlib.W_GZIP;
5551
case ZLIB_OR_NONE:
56-
convertedWrapperType = JZlib.W_ANY;
57-
break;
52+
return JZlib.W_ANY;
5853
default:
59-
throw new Error();
54+
throw new Error("Unexpected wrapper type: " + wrapper);
6055
}
61-
return convertedWrapperType;
6256
}
6357

6458
static int wrapperOverhead(ZlibWrapper wrapper) {
65-
int overhead;
6659
switch (wrapper) {
6760
case NONE:
68-
overhead = 0;
69-
break;
61+
return 0;
7062
case ZLIB:
7163
case ZLIB_OR_NONE:
72-
overhead = 2;
73-
break;
64+
return 2;
7465
case GZIP:
75-
overhead = 10;
76-
break;
66+
return 10;
7767
default:
78-
throw new Error();
68+
throw new Error("Unexpected wrapper type: " + wrapper);
7969
}
80-
return overhead;
8170
}
8271

8372
private ZlibUtil() {

0 commit comments

Comments
 (0)