Skip to content

Commit

Permalink
Make use of ExceptionFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
hiddenalpha committed May 30, 2024
1 parent aaab9d8 commit 46016a2
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ public interface GateleenExceptionFactory {

public Exception newException(String msg, Throwable cause);

/** Convenience overload for {@link #newIllegalStateException(String, Throwable)}. */
public default IllegalStateException newIllegalStateException(String msg) { return newIllegalStateException(msg, null); }

/** Convenience overload for {@link #newIllegalStateException(String, Throwable)}. */
public default IllegalStateException newIllegalStateException(Throwable cause) { return newIllegalStateException(null, cause); }

public IllegalStateException newIllegalStateException(String msg, Throwable cause);

public ReplyException newReplyException(ReplyFailure failureType, int failureCode, String message);


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@ public Exception newException(String message, Throwable cause) {
return new GateleenNoStacktraceException(message, cause);
}

@Override
public IllegalStateException newIllegalStateException(String msg, Throwable cause) {
if (cause instanceof IllegalStateException) return (IllegalStateException) cause;
return new NoStackIllegalStateException(msg);
}

@Override
public ReplyException newReplyException(ReplyFailure failureType, int failureCode, String message) {
return new GateleenNoStackReplyException(failureType, failureCode, message);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ public Exception newException(String message, Throwable cause) {
return new Exception(message, cause);
}

@Override
public IllegalStateException newIllegalStateException(String msg, Throwable cause) {
return new IllegalStateException(msg, cause);
}

@Override
public ReplyException newReplyException(ReplyFailure failureType, int failureCode, String message) {
return new ReplyException(failureType, failureCode, message);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package org.swisspush.gateleen.core.exception;

public class NoStackIllegalStateException extends IllegalStateException {

public NoStackIllegalStateException() {
super();
}

public NoStackIllegalStateException(String s) {
super(s);
}

public NoStackIllegalStateException(String message, Throwable cause) {
super(message, cause);
}

public NoStackIllegalStateException(Throwable cause) {
super(cause);
}

@Override
public Throwable fillInStackTrace() {
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ public void setRoutingContexttHandler(Handler<RoutingContext> wrappedRoutingCont

@Override
protected HttpClientRequest doRequest(HttpMethod method, String uri) {
return new LocalHttpClientRequest(method, uri, vertx, wrappedRoutingContexttHandler, exceptionFactory, new LocalHttpServerResponse(vertx));
return new LocalHttpClientRequest(method, uri, vertx, wrappedRoutingContexttHandler, exceptionFactory, new LocalHttpServerResponse(vertx, exceptionFactory));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import io.vertx.core.http.impl.headers.HeadersMultiMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.swisspush.gateleen.core.exception.GateleenExceptionFactory;
import org.swisspush.gateleen.core.util.StatusCode;

/**
Expand All @@ -16,6 +17,7 @@
public class LocalHttpServerResponse extends BufferBridge implements FastFailHttpServerResponse {

private static final Logger logger = LoggerFactory.getLogger(LocalHttpServerResponse.class);
private final GateleenExceptionFactory exceptionFactory;
private int statusCode;
private String statusMessage;
private static final String EMPTY = "";
Expand Down Expand Up @@ -118,8 +120,9 @@ public HttpClientResponse exceptionHandler(Handler<Throwable> handler) {
};


public LocalHttpServerResponse(Vertx vertx) {
public LocalHttpServerResponse(Vertx vertx, GateleenExceptionFactory exceptionFactory) {
super(vertx);
this.exceptionFactory = exceptionFactory;
// Attach most simple possible exception handler to base.
setExceptionHandler(thr -> logger.error("Processing of response failed.", thr));
}
Expand Down Expand Up @@ -214,9 +217,10 @@ public Future<Void> write(String chunk, String enc) {
public Future<Void> write(Buffer data) {
// emulate Vertx's HttpServerResponseImpl
if (!chunked && !headers.contains(HttpHeaders.CONTENT_LENGTH)) {
IllegalStateException ex = new IllegalStateException("You must set the Content-Length header to be the total size of the message "
IllegalStateException ex = exceptionFactory.newIllegalStateException(""
+ "You must set the Content-Length header to be the total size of the message "
+ "body BEFORE sending any data if you are not using HTTP chunked encoding.");
logger.debug("non-proper HttpServerResponse occured", ex);
logger.debug("non-proper HttpServerResponse occurred", ex);
throw ex;
}
ensureBound();
Expand Down

0 comments on commit 46016a2

Please sign in to comment.