ReceiveCommandErrorHandler.java

  1. /*
  2.  * Copyright (c) 2019, Google LLC  and others
  3.  *
  4.  * This program and the accompanying materials are made available under the
  5.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  6.  * http://www.eclipse.org/org/documents/edl-v10.php.
  7.  *
  8.  * SPDX-License-Identifier: BSD-3-Clause
  9.  */
  10. package org.eclipse.jgit.transport;

  11. import java.io.IOException;
  12. import java.util.List;

  13. import org.eclipse.jgit.errors.MissingObjectException;
  14. import org.eclipse.jgit.transport.ReceiveCommand.Result;

  15. /**
  16.  * Exception handler for processing {@link ReceiveCommand}.
  17.  *
  18.  * @since 5.7
  19.  */
  20. public interface ReceiveCommandErrorHandler {
  21.     /**
  22.      * Handle an exception thrown while validating the new commit ID.
  23.      *
  24.      * @param cmd
  25.      *            offending command
  26.      * @param e
  27.      *            exception thrown
  28.      */
  29.     default void handleNewIdValidationException(ReceiveCommand cmd,
  30.             IOException e) {
  31.         cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd.getNewId().name());
  32.     }

  33.     /**
  34.      * Handle an exception thrown while validating the old commit ID.
  35.      *
  36.      * @param cmd
  37.      *            offending command
  38.      * @param e
  39.      *            exception thrown
  40.      */
  41.     default void handleOldIdValidationException(ReceiveCommand cmd,
  42.             IOException e) {
  43.         cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd.getOldId().name());
  44.     }

  45.     /**
  46.      * Handle an exception thrown while checking if the update is fast-forward.
  47.      *
  48.      * @param cmd
  49.      *            offending command
  50.      * @param e
  51.      *            exception thrown
  52.      */
  53.     default void handleFastForwardCheckException(ReceiveCommand cmd,
  54.             IOException e) {
  55.         if (e instanceof MissingObjectException) {
  56.             cmd.setResult(Result.REJECTED_MISSING_OBJECT, e.getMessage());
  57.         } else {
  58.             cmd.setResult(Result.REJECTED_OTHER_REASON);
  59.         }
  60.     }

  61.     /**
  62.      * Handle an exception thrown while checking if the update is fast-forward.
  63.      *
  64.      * @param cmds
  65.      *            commands being processed
  66.      * @param e
  67.      *            exception thrown
  68.      */
  69.     default void handleBatchRefUpdateException(List<ReceiveCommand> cmds,
  70.             IOException e) {
  71.         for (ReceiveCommand cmd : cmds) {
  72.             if (cmd.getResult() == Result.NOT_ATTEMPTED) {
  73.                 cmd.reject(e);
  74.             }
  75.         }
  76.     }
  77. }