View Javadoc
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  
12  import java.io.IOException;
13  import java.util.List;
14  
15  import org.eclipse.jgit.errors.MissingObjectException;
16  import org.eclipse.jgit.transport.ReceiveCommand.Result;
17  
18  /**
19   * Exception handler for processing {@link ReceiveCommand}.
20   *
21   * @since 5.7
22   */
23  public interface ReceiveCommandErrorHandler {
24  	/**
25  	 * Handle an exception thrown while validating the new commit ID.
26  	 *
27  	 * @param cmd
28  	 *            offending command
29  	 * @param e
30  	 *            exception thrown
31  	 */
32  	default void handleNewIdValidationException(ReceiveCommand cmd,
33  			IOException e) {
34  		cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd.getNewId().name());
35  	}
36  
37  	/**
38  	 * Handle an exception thrown while validating the old commit ID.
39  	 *
40  	 * @param cmd
41  	 *            offending command
42  	 * @param e
43  	 *            exception thrown
44  	 */
45  	default void handleOldIdValidationException(ReceiveCommand cmd,
46  			IOException e) {
47  		cmd.setResult(Result.REJECTED_MISSING_OBJECT, cmd.getOldId().name());
48  	}
49  
50  	/**
51  	 * Handle an exception thrown while checking if the update is fast-forward.
52  	 *
53  	 * @param cmd
54  	 *            offending command
55  	 * @param e
56  	 *            exception thrown
57  	 */
58  	default void handleFastForwardCheckException(ReceiveCommand cmd,
59  			IOException e) {
60  		if (e instanceof MissingObjectException) {
61  			cmd.setResult(Result.REJECTED_MISSING_OBJECT, e.getMessage());
62  		} else {
63  			cmd.setResult(Result.REJECTED_OTHER_REASON);
64  		}
65  	}
66  
67  	/**
68  	 * Handle an exception thrown while checking if the update is fast-forward.
69  	 *
70  	 * @param cmds
71  	 *            commands being processed
72  	 * @param e
73  	 *            exception thrown
74  	 */
75  	default void handleBatchRefUpdateException(List<ReceiveCommand> cmds,
76  			IOException e) {
77  		for (ReceiveCommand cmd : cmds) {
78  			if (cmd.getResult() == Result.NOT_ATTEMPTED) {
79  				cmd.reject(e);
80  			}
81  		}
82  	}
83  }