You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2021/12/17 11:11:32 UTC

[httpcomponents-core] 01/01: Use subclass of ConnectionClosedException to signal request execution failures due to the connection being closed. Requests failed with this exception should generally be safe to re-execute

This is an automated email from the ASF dual-hosted git repository.

olegk pushed a commit to branch HTTPCORE-705
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit bd5c6990b524e021bde6307c8451f7fd13812241
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Fri Dec 17 12:11:15 2021 +0100

    Use subclass of ConnectionClosedException to signal request execution failures due to the connection being closed. Requests failed with this exception should generally be safe to re-execute
---
 .../hc/core5/http/RequestNotExecutedException.java | 56 ++++++++++++++++++++++
 .../hc/core5/http/nio/command/CommandSupport.java  |  4 +-
 .../http/nio/command/RequestExecutionCommand.java  |  3 +-
 3 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/RequestNotExecutedException.java b/httpcore5/src/main/java/org/apache/hc/core5/http/RequestNotExecutedException.java
new file mode 100644
index 0000000..3f7e70a
--- /dev/null
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/RequestNotExecutedException.java
@@ -0,0 +1,56 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF 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
+ *
+ *   http://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.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http;
+
+import org.apache.hc.core5.annotation.Internal;
+
+/**
+ * {@link ConnectionClosedException} subclass that signals requests cannot not be executed
+ * due to the connection being closed. Generally requests failed with this exception
+ * are safe to be re-executed.
+ */
+@Internal
+public class RequestNotExecutedException extends ConnectionClosedException {
+
+    /**
+     * Constructs a new RequestNotExecutedException with a default message.
+     */
+    public RequestNotExecutedException() {
+        super("Connection is closed");
+    }
+
+    /**
+     * Constructs a new RequestNotExecutedException with the specified detail message.
+     *
+     * @param message The exception detail message
+     */
+    public RequestNotExecutedException(final String message) {
+        super(message);
+    }
+
+}
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/CommandSupport.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/CommandSupport.java
index c2facea..9901e4c 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/CommandSupport.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/CommandSupport.java
@@ -28,7 +28,7 @@
 package org.apache.hc.core5.http.nio.command;
 
 import org.apache.hc.core5.annotation.Internal;
-import org.apache.hc.core5.http.ConnectionClosedException;
+import org.apache.hc.core5.http.RequestNotExecutedException;
 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.reactor.Command;
 import org.apache.hc.core5.reactor.IOSession;
@@ -73,7 +73,7 @@ public final class CommandSupport {
                 final AsyncClientExchangeHandler exchangeHandler = ((RequestExecutionCommand) command).getExchangeHandler();
                 try {
                     if (!ioSession.isOpen()) {
-                        exchangeHandler.failed(new ConnectionClosedException());
+                        exchangeHandler.failed(new RequestNotExecutedException());
                     } else {
                         exchangeHandler.cancel();
                     }
diff --git a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/RequestExecutionCommand.java b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/RequestExecutionCommand.java
index 05f0961..295b181 100644
--- a/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/RequestExecutionCommand.java
+++ b/httpcore5/src/main/java/org/apache/hc/core5/http/nio/command/RequestExecutionCommand.java
@@ -29,6 +29,7 @@ package org.apache.hc.core5.http.nio.command;
 
 import org.apache.hc.core5.annotation.Internal;
 import org.apache.hc.core5.concurrent.CancellableDependency;
+import org.apache.hc.core5.http.RequestNotExecutedException;
 import org.apache.hc.core5.http.nio.AsyncClientExchangeHandler;
 import org.apache.hc.core5.http.nio.AsyncPushConsumer;
 import org.apache.hc.core5.http.nio.HandlerFactory;
@@ -100,7 +101,7 @@ public final class RequestExecutionCommand extends ExecutableCommand {
 
     @Override
     public boolean cancel() {
-        exchangeHandler.cancel();
+        exchangeHandler.failed(new RequestNotExecutedException());
         return true;
     }