Skip to content

Commit be61af4

Browse files
core: use RESOURCE_EXHAUSTED for max message size failures
1 parent 4f96b0a commit be61af4

File tree

8 files changed

+17
-16
lines changed

8 files changed

+17
-16
lines changed

core/src/main/java/io/grpc/internal/AbstractClientStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ protected void inboundDeliveryPaused() {
172172

173173
@Override
174174
protected final void deframeFailed(Throwable cause) {
175-
cancel(Status.INTERNAL.withDescription("Exception deframing message").withCause(cause));
175+
cancel(Status.fromThrowable(cause));
176176
}
177177

178178
/**

core/src/main/java/io/grpc/internal/MessageDeframer.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,8 +344,10 @@ private void processHeader() {
344344
// Update the required length to include the length of the frame.
345345
requiredLength = nextFrame.readInt();
346346
if (requiredLength < 0 || requiredLength > maxInboundMessageSize) {
347-
throw Status.INTERNAL.withDescription(String.format("%s: Frame size %d exceeds maximum: %d. ",
348-
debugString, requiredLength, maxInboundMessageSize)).asRuntimeException();
347+
throw Status.RESOURCE_EXHAUSTED.withDescription(
348+
String.format("%s: Frame size %d exceeds maximum: %d. ",
349+
debugString, requiredLength, maxInboundMessageSize))
350+
.asRuntimeException();
349351
}
350352

351353
statsTraceCtx.inboundMessage();
@@ -469,7 +471,7 @@ private void reportCount() {
469471

470472
private void verifySize() {
471473
if (count > maxMessageSize) {
472-
throw Status.INTERNAL.withDescription(String.format(
474+
throw Status.RESOURCE_EXHAUSTED.withDescription(String.format(
473475
"%s: Compressed frame exceeds maximum frame size: %d. Bytes read: %d. ",
474476
debugString, maxMessageSize, count)).asRuntimeException();
475477
}

core/src/main/java/io/grpc/internal/MessageFramer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ private int writeUncompressed(InputStream message, int messageLength) throws IOE
169169
BufferChainOutputStream bufferChain = new BufferChainOutputStream();
170170
int written = writeToOutputStream(message, bufferChain);
171171
if (maxOutboundMessageSize >= 0 && written > maxOutboundMessageSize) {
172-
throw Status.INTERNAL
172+
throw Status.RESOURCE_EXHAUSTED
173173
.withDescription(
174174
String.format("message too large %d > %d", written , maxOutboundMessageSize))
175175
.asRuntimeException();
@@ -189,7 +189,7 @@ private int writeCompressed(InputStream message, int unusedMessageLength) throws
189189
compressingStream.close();
190190
}
191191
if (maxOutboundMessageSize >= 0 && written > maxOutboundMessageSize) {
192-
throw Status.CANCELLED
192+
throw Status.RESOURCE_EXHAUSTED
193193
.withDescription(
194194
String.format("message too large %d > %d", written , maxOutboundMessageSize))
195195
.asRuntimeException();
@@ -212,7 +212,7 @@ private int getKnownLength(InputStream inputStream) throws IOException {
212212
private int writeKnownLengthUncompressed(InputStream message, int messageLength)
213213
throws IOException {
214214
if (maxOutboundMessageSize >= 0 && messageLength > maxOutboundMessageSize) {
215-
throw Status.CANCELLED
215+
throw Status.RESOURCE_EXHAUSTED
216216
.withDescription(
217217
String.format("message too large %d > %d", messageLength , maxOutboundMessageSize))
218218
.asRuntimeException();

core/src/test/java/io/grpc/internal/AbstractClientStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ protected void sendCancel(Status errorStatus) {
155155
stream.deframeFailed(new RuntimeException("something bad"));
156156

157157
verify(mockListener).closed(statusCaptor.capture(), isA(Metadata.class));
158-
assertEquals(Code.INTERNAL, statusCaptor.getValue().getCode());
158+
assertEquals(Code.UNKNOWN, statusCaptor.getValue().getCode());
159159
}
160160

161161
@Test

core/src/test/java/io/grpc/internal/MessageDeframerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public void sizeEnforcingInputStream_readByteAboveLimit() throws IOException {
269269

270270
try {
271271
thrown.expect(StatusRuntimeException.class);
272-
thrown.expectMessage("INTERNAL: test: Compressed frame exceeds");
272+
thrown.expectMessage("RESOURCE_EXHAUSTED: test: Compressed frame exceeds");
273273

274274
while (stream.read() != -1) {}
275275
} finally {
@@ -316,7 +316,7 @@ public void sizeEnforcingInputStream_readAboveLimit() throws IOException {
316316

317317
try {
318318
thrown.expect(StatusRuntimeException.class);
319-
thrown.expectMessage("INTERNAL: test: Compressed frame exceeds");
319+
thrown.expectMessage("RESOURCE_EXHAUSTED: test: Compressed frame exceeds");
320320

321321
stream.read(buf, 0, buf.length);
322322
} finally {
@@ -361,7 +361,7 @@ public void sizeEnforcingInputStream_skipAboveLimit() throws IOException {
361361

362362
try {
363363
thrown.expect(StatusRuntimeException.class);
364-
thrown.expectMessage("INTERNAL: test: Compressed frame exceeds");
364+
thrown.expectMessage("RESOURCE_EXHAUSTED: test: Compressed frame exceeds");
365365

366366
stream.skip(4);
367367
} finally {

interop-testing/src/main/java/io/grpc/testing/integration/AbstractInteropTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -906,7 +906,7 @@ public void maxInboundSize_tooBig() {
906906
fail();
907907
} catch (StatusRuntimeException ex) {
908908
Status s = ex.getStatus();
909-
assertThat(s.getCode()).named(s.toString()).isEqualTo(Status.Code.INTERNAL);
909+
assertThat(s.getCode()).named(s.toString()).isEqualTo(Status.Code.RESOURCE_EXHAUSTED);
910910
assertThat(Throwables.getStackTraceAsString(ex)).contains("exceeds maximum");
911911
}
912912
}

netty/src/test/java/io/grpc/netty/NettyClientTransportTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
package io.grpc.netty;
3333

3434
import static com.google.common.base.Charsets.UTF_8;
35-
import static io.grpc.Status.Code.INTERNAL;
3635
import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
3736
import static io.grpc.internal.GrpcUtil.DEFAULT_SERVER_KEEPALIVE_TIMEOUT_NANOS;
3837
import static io.grpc.internal.GrpcUtil.DEFAULT_SERVER_KEEPALIVE_TIME_NANOS;
@@ -58,6 +57,7 @@
5857
import io.grpc.MethodDescriptor.Marshaller;
5958
import io.grpc.ServerStreamTracer;
6059
import io.grpc.Status;
60+
import io.grpc.Status.Code;
6161
import io.grpc.StatusException;
6262
import io.grpc.internal.ClientStream;
6363
import io.grpc.internal.ClientStreamListener;
@@ -213,7 +213,7 @@ public void maxMessageSizeShouldBeEnforced() throws Throwable {
213213
fail("Expected the stream to fail.");
214214
} catch (ExecutionException e) {
215215
Status status = Status.fromThrowable(e);
216-
assertEquals(INTERNAL, status.getCode());
216+
assertEquals(Code.RESOURCE_EXHAUSTED, status.getCode());
217217
assertTrue("Missing exceeds maximum from: " + status.getDescription(),
218218
status.getDescription().contains("exceeds maximum"));
219219
}

okhttp/src/test/java/io/grpc/okhttp/OkHttpClientTransportTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333

3434
import static com.google.common.base.Charsets.UTF_8;
3535
import static com.google.common.truth.Truth.assertThat;
36-
import static io.grpc.Status.Code.INTERNAL;
3736
import static io.grpc.internal.GrpcUtil.DEFAULT_MAX_MESSAGE_SIZE;
3837
import static io.grpc.okhttp.Headers.CONTENT_TYPE_HEADER;
3938
import static io.grpc.okhttp.Headers.METHOD_HEADER;
@@ -218,7 +217,7 @@ public void maxMessageSizeShouldBeEnforced() throws Exception {
218217
frameHandler().data(false, 3, buffer, (int) buffer.size());
219218

220219
listener.waitUntilStreamClosed();
221-
assertEquals(INTERNAL, listener.status.getCode());
220+
assertEquals(Code.RESOURCE_EXHAUSTED, listener.status.getCode());
222221
shutdownAndVerify();
223222
}
224223

0 commit comments

Comments
 (0)