From b8d9734c0268446cddac281ec762808ac369538e Mon Sep 17 00:00:00 2001 From: Masaya Suzuki Date: Tue, 6 Aug 2019 11:12:43 -0700 Subject: [PATCH] UploadPack: Create a method that propagates an exception as-is 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 --- .../eclipse/jgit/transport/UploadPack.java | 98 ++++++++++++------- 1 file changed, 64 insertions(+), 34 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java index 8d725b0b58..9c4e000c75 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/UploadPack.java @@ -759,9 +759,59 @@ public class UploadPack { /** * Execute the upload task on the socket. * - *

If the client passed extra parameters (e.g., "version=2") through a - * side channel, the caller must call setExtraParameters first to supply - * them. + *

+ * Same as {@link #uploadWithExceptionPropagation} except that the thrown + * exceptions are handled in the method, and the error messages are sent to + * the clients. + * + *

+ * 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. + * + *

+ * 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(); -- 2.39.5