diff --git a/core/src/main/java/com/linecorp/armeria/common/ContentTooLargeException.java b/core/src/main/java/com/linecorp/armeria/common/ContentTooLargeException.java index bc5c6d0f64d..f360f36e95e 100644 --- a/core/src/main/java/com/linecorp/armeria/common/ContentTooLargeException.java +++ b/core/src/main/java/com/linecorp/armeria/common/ContentTooLargeException.java @@ -18,11 +18,12 @@ import com.linecorp.armeria.common.annotation.Nullable; import com.linecorp.armeria.internal.common.util.TemporaryThreadLocals; +import com.linecorp.armeria.server.HttpStatusException; /** * A {@link RuntimeException} raised when the length of request or response content exceeds its limit. */ -public final class ContentTooLargeException extends RuntimeException { +public final class ContentTooLargeException extends HttpStatusException { private static final long serialVersionUID = 4901614315474105954L; @@ -56,7 +57,7 @@ private ContentTooLargeException() { } private ContentTooLargeException(boolean neverSample) { - super(null, null, !neverSample, !neverSample); + super(HttpStatus.REQUEST_ENTITY_TOO_LARGE, !neverSample, null); this.neverSample = neverSample; maxContentLength = -1; @@ -66,7 +67,8 @@ private ContentTooLargeException(boolean neverSample) { ContentTooLargeException(long maxContentLength, long contentLength, long transferred, @Nullable Throwable cause) { - super(toString(maxContentLength, contentLength, transferred), cause); + super(HttpStatus.REQUEST_ENTITY_TOO_LARGE, + toString(maxContentLength, contentLength, transferred), cause); neverSample = false; this.transferred = transferred; diff --git a/core/src/main/java/com/linecorp/armeria/server/HttpStatusException.java b/core/src/main/java/com/linecorp/armeria/server/HttpStatusException.java index 1a81ffc4976..f7f284125ae 100644 --- a/core/src/main/java/com/linecorp/armeria/server/HttpStatusException.java +++ b/core/src/main/java/com/linecorp/armeria/server/HttpStatusException.java @@ -59,7 +59,7 @@ * * @see HttpResponseException */ -public final class HttpStatusException extends RuntimeException { +public class HttpStatusException extends RuntimeException { private static final HttpStatusException[] EXCEPTIONS = new HttpStatusException[1000]; @@ -120,11 +120,19 @@ private static HttpStatusException of0(HttpStatus status, @Nullable Throwable ca /** * Creates a new instance. */ - private HttpStatusException(HttpStatus httpStatus, boolean withStackTrace, @Nullable Throwable cause) { + protected HttpStatusException(HttpStatus httpStatus, boolean withStackTrace, @Nullable Throwable cause) { super(requireNonNull(httpStatus, "httpStatus").toString(), cause, withStackTrace, withStackTrace); this.httpStatus = httpStatus; } + /** + * Creates a new instance. + */ + protected HttpStatusException(HttpStatus httpStatus, @Nullable String message, @Nullable Throwable cause) { + super(message == null ? requireNonNull(httpStatus, "httpStatus").toString() : message, cause); + this.httpStatus = requireNonNull(httpStatus, "httpStatus"); + } + /** * Returns the {@link HttpStatus} which would be sent back to the client who sent the * corresponding request. diff --git a/core/src/test/java/com/linecorp/armeria/common/ContentTooLargeExceptionTest.java b/core/src/test/java/com/linecorp/armeria/common/ContentTooLargeExceptionTest.java new file mode 100644 index 00000000000..4b499f00e9e --- /dev/null +++ b/core/src/test/java/com/linecorp/armeria/common/ContentTooLargeExceptionTest.java @@ -0,0 +1,34 @@ +/* + * Copyright 2024 LINE Corporation + * + * LINE Corporation licenses this file to you under the Apache License, + * version 2.0 (the "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at: + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the + * License for the specific language governing permissions and limitations + * under the License. + */ + +package com.linecorp.armeria.common; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; + +import org.junit.jupiter.api.Test; + +import com.linecorp.armeria.server.HttpStatusException; + +class ContentTooLargeExceptionTest { + @Test + void instanceOfHttpStatusException() { + + final ContentTooLargeException cause = ContentTooLargeException.get(); + assertInstanceOf(HttpStatusException.class, cause); + assertEquals(HttpStatus.REQUEST_ENTITY_TOO_LARGE, cause.httpStatus()); + } +}