Skip to content

Commit 87dd5e9

Browse files
predic8rrayst
andauthored
Handle specific exceptions in HTTPClientInterceptor with detailed responses (#2945)
Co-authored-by: Tobias Polley <polley@predic8.de>
1 parent f44812c commit 87dd5e9

1 file changed

Lines changed: 39 additions & 11 deletions

File tree

core/src/main/java/com/predic8/membrane/core/interceptor/HTTPClientInterceptor.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,26 @@
1313
limitations under the License. */
1414
package com.predic8.membrane.core.interceptor;
1515

16-
import com.predic8.membrane.annot.*;
17-
import com.predic8.membrane.core.exchange.*;
18-
import com.predic8.membrane.core.proxies.*;
19-
import com.predic8.membrane.core.transport.http.*;
20-
import com.predic8.membrane.core.transport.http.client.*;
21-
import com.predic8.membrane.core.util.*;
22-
import org.slf4j.*;
16+
import com.predic8.membrane.annot.MCAttribute;
17+
import com.predic8.membrane.annot.MCChildElement;
18+
import com.predic8.membrane.annot.MCElement;
19+
import com.predic8.membrane.core.exchange.Exchange;
20+
import com.predic8.membrane.core.proxies.AbstractServiceProxy;
21+
import com.predic8.membrane.core.transport.http.EOFWhileReadingLineException;
22+
import com.predic8.membrane.core.transport.http.HttpClient;
23+
import com.predic8.membrane.core.transport.http.ProtocolUpgradeDeniedException;
24+
import com.predic8.membrane.core.transport.http.UnableToTunnelException;
25+
import com.predic8.membrane.core.transport.http.client.HttpClientConfiguration;
26+
import com.predic8.membrane.core.util.URLUtil;
27+
import org.slf4j.Logger;
28+
import org.slf4j.LoggerFactory;
2329

2430
import java.net.*;
2531

2632
import static com.predic8.membrane.core.exceptions.ProblemDetails.*;
27-
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.Set.*;
28-
import static com.predic8.membrane.core.interceptor.Outcome.*;
33+
import static com.predic8.membrane.core.interceptor.Interceptor.Flow.Set.REQUEST_FLOW;
34+
import static com.predic8.membrane.core.interceptor.Outcome.ABORT;
35+
import static com.predic8.membrane.core.interceptor.Outcome.RETURN;
2936

3037
/**
3138
* @description The <i>httpClient</i> sends the request of an exchange to a Web
@@ -83,19 +90,40 @@ public Outcome handleRequest(Exchange exc) {
8390
httpClient.call(exc);
8491
return RETURN;
8592
} catch (ConnectException e) {
86-
String msg = "Target %s is not reachable.".formatted(getDestination(exc));
93+
var msg = "Target %s is not reachable.".formatted(getDestination(exc));
8794
log.warn("{} {}", msg, PROXIES_HINT);
8895
gateway(router.getConfiguration().isProduction(), getDisplayName())
96+
.title("Bad Gateway")
8997
.addSubSee("connect")
9098
.status(502)
9199
.detail(msg)
92100
.buildAndSetResponse(exc);
93101
return ABORT;
102+
} catch (SocketException e) {
103+
internal(router.getConfiguration().isProduction(), getDisplayName())
104+
.title("Bad Gateway")
105+
.status(502)
106+
.addSubSee("socket-exception")
107+
.detail("Error communicating with target %s. Reason: %s".formatted(exc.getDestinations(), e.getMessage()))
108+
.stacktrace(false)
109+
.buildAndSetResponse(exc);
110+
return ABORT;
111+
} catch (EOFWhileReadingLineException e) {
112+
internal(router.getConfiguration().isProduction(), getDisplayName())
113+
.title("Bad Gateway")
114+
.status(502)
115+
.detail("Backend closed the connection before the response was complete.")
116+
.addSubSee("socket-closed")
117+
.stacktrace(false)
118+
.buildAndSetResponse(exc);
119+
return ABORT;
94120
} catch (SocketTimeoutException e) {
95121
// Details are logged further down in the HTTPClient
122+
log.info("Target {} is not reachable.",exc.getDestinations());
96123
internal(router.getConfiguration().isProduction(), getDisplayName())
124+
.title("Gateway Timeout")
125+
.status(504)
97126
.addSubSee("socket-timeout")
98-
.detail("Target %s is not reachable.".formatted(exc.getDestinations()))
99127
.buildAndSetResponse(exc);
100128
return ABORT;
101129
} catch (UnknownHostException e) {

0 commit comments

Comments
 (0)