]> source.dussan.org Git - jgit.git/commitdiff
UploadPack: Create a method that propagates an exception as-is 43/147143/8
authorMasaya Suzuki <masayasuzuki@google.com>
Tue, 6 Aug 2019 18:12:43 +0000 (11:12 -0700)
committerMasaya Suzuki <masayasuzuki@google.com>
Wed, 9 Oct 2019 17:02:03 +0000 (10:02 -0700)
Exception handling can be isolated from UploadPack. This makes it
possible to make the exception handler pluggable.

Change-Id: Ieebbd6711963c7f2e47a98783b4ad815793721c7
Signed-off-by: Masaya Suzuki <masayasuzuki@google.com>
org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java

index 8d725b0b585f5b2204dd4b702422739464c7744d..9c4e000c75a991f7114d18ad613edf4b11411ad4 100644 (file)
@@ -759,9 +759,59 @@ public class UploadPack {
        /**
         * Execute the upload task on the socket.
         *
-        * <p>If the client passed extra parameters (e.g., "version=2") through a
-        * side channel, the caller must call setExtraParameters first to supply
-        * them.
+        * <p>
+        * Same as {@link #uploadWithExceptionPropagation} except that the thrown
+        * exceptions are handled in the method, and the error messages are sent to
+        * the clients.
+        *
+        * <p>
+        * Call this method if the caller does not have an error handling mechanism.
+        * Call {@link #uploadWithExceptionPropagation} if the caller wants to have
+        * its own error handling mechanism.
+        *
+        * @param input
+        * @param output
+        * @param messages
+        * @throws java.io.IOException
+        */
+       public void upload(InputStream input, OutputStream output,
+                       @Nullable OutputStream messages) throws IOException {
+               try {
+                       uploadWithExceptionPropagation(input, output, messages);
+               } catch (ServiceMayNotContinueException err) {
+                       if (!err.isOutput() && err.getMessage() != null) {
+                               try {
+                                       errOut.writeError(err.getMessage());
+                               } catch (IOException e) {
+                                       err.addSuppressed(e);
+                                       throw err;
+                               }
+                               err.setOutput();
+                       }
+                       throw err;
+               } catch (IOException | RuntimeException | Error err) {
+                       if (rawOut != null) {
+                               String msg = err instanceof PackProtocolException
+                                               ? err.getMessage()
+                                               : JGitText.get().internalServerError;
+                               try {
+                                       errOut.writeError(msg);
+                               } catch (IOException e) {
+                                       err.addSuppressed(e);
+                                       throw err;
+                               }
+                               throw new UploadPackInternalServerErrorException(err);
+                       }
+                       throw err;
+               }
+       }
+
+       /**
+        * Execute the upload task on the socket.
+        *
+        * <p>
+        * If the client passed extra parameters (e.g., "version=2") through a side
+        * channel, the caller must call setExtraParameters first to supply them.
         *
         * @param input
         *            raw input to read client commands from. Caller must ensure the
@@ -775,15 +825,20 @@ public class UploadPack {
         *            through. When run over SSH this should be tied back to the
         *            standard error channel of the command execution. For most
         *            other network connections this should be null.
-        * @throws java.io.IOException
+        * @throws ServiceMayNotContinueException
+        *             thrown if one of the hooks throws this.
+        * @throws IOException
+        *             thrown if the server or the client I/O fails, or there's an
+        *             internal server error.
         */
-       public void upload(InputStream input, OutputStream output,
-                       @Nullable OutputStream messages) throws IOException {
-               PacketLineOut pckOut = null;
+       public void uploadWithExceptionPropagation(InputStream input,
+                       OutputStream output, @Nullable OutputStream messages)
+                       throws ServiceMayNotContinueException, IOException {
                try {
                        rawIn = input;
-                       if (messages != null)
+                       if (messages != null) {
                                msgOut = messages;
+                       }
 
                        if (timeout > 0) {
                                final Thread caller = Thread.currentThread();
@@ -803,37 +858,12 @@ public class UploadPack {
                        }
 
                        pckIn = new PacketLineIn(rawIn);
-                       pckOut = new PacketLineOut(rawOut);
+                       PacketLineOut pckOut = new PacketLineOut(rawOut);
                        if (useProtocolV2()) {
                                serviceV2(pckOut);
                        } else {
                                service(pckOut);
                        }
-               } catch (ServiceMayNotContinueException err) {
-                       if (!err.isOutput() && err.getMessage() != null) {
-                               try {
-                                       errOut.writeError(err.getMessage());
-                               } catch (IOException e) {
-                                       err.addSuppressed(e);
-                                       throw err;
-                               }
-                               err.setOutput();
-                       }
-                       throw err;
-               } catch (IOException | RuntimeException | Error err) {
-                       if (rawOut != null) {
-                               String msg = err instanceof PackProtocolException
-                                               ? err.getMessage()
-                                               : JGitText.get().internalServerError;
-                               try {
-                                       errOut.writeError(msg);
-                               } catch (IOException e) {
-                                       err.addSuppressed(e);
-                                       throw err;
-                               }
-                               throw new UploadPackInternalServerErrorException(err);
-                       }
-                       throw err;
                } finally {
                        msgOut = NullOutputStream.INSTANCE;
                        walk.close();