Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.eclipse.jetty.client.transport.internal.HttpChannelOverHTTP;
import org.eclipse.jetty.client.transport.internal.HttpConnectionOverHTTP;
import org.eclipse.jetty.client.transport.internal.HttpReceiverOverHTTP;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpCompliance;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.HttpFields;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpVersion;
Expand Down Expand Up @@ -226,7 +226,7 @@ public void testReceiveBadResponse(HttpCompliance compliance) throws Exception

ExecutionException e = assertThrows(ExecutionException.class, () -> completable.get(5, TimeUnit.SECONDS));
assertThat(e.getCause(), instanceOf(HttpResponseException.class));
assertThat(e.getCause().getCause(), instanceOf(BadMessageException.class));
assertThat(e.getCause().getCause(), instanceOf(HttpException.class));
assertThat(e.getCause().getCause().getCause(), instanceOf(NumberFormatException.class));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
import org.eclipse.jetty.compression.Compression;
import org.eclipse.jetty.compression.server.internal.CompressionResponse;
import org.eclipse.jetty.compression.server.internal.DecompressionRequest;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.ComplianceViolation;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.HttpField;
Expand Down Expand Up @@ -280,7 +279,7 @@ protected void onComplianceViolation(ComplianceViolation violation, String value
if (httpConfiguration.getHttpCompliance().allows(violation))
httpConfiguration.notifyViolation(violation, value);
else
throw new BadMessageException(violation.toString());
throw new HttpException.IllegalStateException(HttpStatus.BAD_REQUEST_400, violation.toString());
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
import org.eclipse.jetty.fcgi.generator.Flusher;
import org.eclipse.jetty.fcgi.generator.ServerGenerator;
import org.eclipse.jetty.fcgi.parser.ServerParser;
import org.eclipse.jetty.http.BadMessageException;
import org.eclipse.jetty.http.HttpException;
import org.eclipse.jetty.http.HttpField;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.http.HttpVersion;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.Content;
Expand Down Expand Up @@ -401,7 +402,7 @@ public void onFailure(int request, Throwable failure)
if (LOG.isDebugEnabled())
LOG.atDebug().setCause(failure).log("Request {} failure on {}", request, stream);
if (stream != null)
ThreadPool.executeImmediately(getExecutor(), stream.getHttpChannel().onFailure(new BadMessageException(null, failure)));
ThreadPool.executeImmediately(getExecutor(), stream.getHttpChannel().onFailure(new HttpException.IllegalStateException(HttpStatus.BAD_REQUEST_400, null, failure)));
stream = null;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@

/**
* <p>Exception thrown to indicate a Bad HTTP Message has either been received
* or attempted to be generated. Typically these are handled with either 400
* or attempted to be generated. Typically, these are handled with {@code 4xx} or {@code 5xx}
* responses.</p>
* @deprecated use HttpException
*/
@Deprecated(since = "12.1.6", forRemoval = true)
public class BadMessageException extends HttpException.RuntimeException
{
public BadMessageException()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public List<HttpCookie> getCookies(HttpFields headers, ComplianceViolation.Liste
}
catch (CookieParser.InvalidCookieException invalidCookieException)
{
throw new BadMessageException(invalidCookieException.getMessage(), invalidCookieException);
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, invalidCookieException.getMessage(), invalidCookieException);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ protected HostPortHttpField(HttpHeader header, String name, String authority)
}
catch (Exception e)
{
throw new BadMessageException("Bad HostPort", e);
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, "Bad HostPort", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -456,6 +456,6 @@ private static void assertAllowed(Violation violation, HttpCompliance mode, Comp
mode, violation, violation.getDescription()
));
else
throw new BadMessageException(violation.getDescription());
throw new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, violation.getDescription());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import org.eclipse.jetty.util.ExceptionUtil;

/**
* <p>A tagging interface for Exceptions that carry a HTTP response code and reason.</p>
* <p>Exception sub-classes that implement this interface will be caught by the container
* <p>A tagging interface for Exceptions that carry an HTTP response code and reason.</p>
* <p>Exception subclasses that implement this interface will be caught by the container
* and the {@link #getCode()} used to send a response.</p>
*/
public interface HttpException extends QuietException
Expand All @@ -27,15 +27,58 @@ public interface HttpException extends QuietException

String getReason();

static HttpException asHttpException(Throwable throwable)
{
if (throwable instanceof IllegalArgumentException iae)
return iae;
if (throwable instanceof IllegalStateException ise)
return ise;
if (throwable instanceof RuntimeException re)
return re;
if (throwable instanceof java.lang.IllegalArgumentException)
return new HttpException.IllegalArgumentException(HttpStatus.BAD_REQUEST_400, throwable.getMessage(), throwable);
if (throwable instanceof java.lang.IllegalStateException)
return new HttpException.IllegalStateException(HttpStatus.BAD_REQUEST_400, throwable.getMessage(), throwable);
if (throwable instanceof java.lang.RuntimeException)
return new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, throwable.getMessage(), throwable);
return new HttpException.RuntimeException(HttpStatus.BAD_REQUEST_400, throwable.getMessage(), throwable);
}

/**
* Throw the given HttpException as an unchecked exception.
*
* @param httpException the HttpException to throw
*/
static void throwAsUnchecked(HttpException httpException)
{
ExceptionUtil.ifExceptionThrowUnchecked((Throwable)httpException);
if (httpException instanceof Throwable throwable)
ExceptionUtil.ifExceptionThrowUnchecked(throwable);
throw new IllegalStateException(httpException.getCode(), httpException.getReason());
}

/**
* Convert the given Throwable to an HttpException and throw it as an unchecked exception.
*
* @param throwable the Throwable to convert and throw
*/
static void throwAsUncheckedHttpException(Throwable throwable)
{
throwAsUnchecked(asHttpException(throwable));
}

/**
* If the given Throwable is an HttpException, throw it as an unchecked exception.
*
* @param th the Throwable to check and possibly throw
*/
static void throwIfHttpException(Throwable th)
{
if (th instanceof HttpException he)
HttpException.throwAsUnchecked(he);
}

/**
* <p>Exception thrown to indicate a Bad HTTP Message has either been received
* or attempted to be generated. Typically these are handled with either 400
* or 500 responses.</p>
* A RuntimeException version of HttpException.
*/
class RuntimeException extends java.lang.RuntimeException implements HttpException
{
Expand All @@ -60,6 +103,7 @@ public RuntimeException(int code, Throwable cause)
public RuntimeException(int code, String reason, Throwable cause)
{
super(code + ": " + reason, cause);
assert code == 0 || HttpStatus.isClientError(code) || HttpStatus.isServerError(code);
_code = code;
_reason = reason;
}
Expand All @@ -75,12 +119,15 @@ public String getReason()
{
return _reason;
}

public IllegalStateException asIllegalStateException()
{
return new IllegalStateException(_code, _reason, getCause());
}
}

/**
* <p>Exception thrown to indicate a Bad HTTP Message has either been received
* or attempted to be generated. Typically these are handled with either 400
* or 500 responses.</p>
* An IllegalArgumentException version of HttpException.
*/
class IllegalArgumentException extends java.lang.IllegalArgumentException implements HttpException
{
Expand All @@ -100,6 +147,46 @@ public IllegalArgumentException(int code, String reason)
public IllegalArgumentException(int code, String reason, Throwable cause)
{
super(code + ": " + reason, cause);
assert code == 0 || HttpStatus.isClientError(code) || HttpStatus.isServerError(code);
_code = code;
_reason = reason;
}

@Override
public int getCode()
{
return _code;
}

@Override
public String getReason()
{
return _reason;
}
}

/**
* An IllegalStateException version of HttpException.
*/
class IllegalStateException extends java.lang.IllegalStateException implements HttpException
{
private final int _code;
private final String _reason;

public IllegalStateException(int code)
{
this(code, null, null);
}

public IllegalStateException(int code, String reason)
{
this(code, reason, null);
}

public IllegalStateException(int code, String reason, Throwable cause)
{
super(code + ": " + reason, cause);
assert code == 0 || HttpStatus.isClientError(code) || HttpStatus.isServerError(code);
_code = code;
_reason = reason;
}
Expand Down
Loading