Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue-5578] make ContentTooLargeException extend HttpStatusException #5624

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}