View Javadoc
1   /*
2    * Copyright (C) 2018-2021, Andre Bossert <andre.bossert@siemens.com>
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    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.diffmergetool;
12  
13  import org.eclipse.jgit.util.FS.ExecutionResult;
14  
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  /**
19   * Tool exception for differentiation.
20   *
21   */
22  public class ToolException extends Exception {
23  
24  	private final static Logger LOG = LoggerFactory
25  			.getLogger(ToolException.class);
26  
27  	private final ExecutionResult result;
28  
29  	private final boolean commandExecutionError;
30  
31  	/**
32  	 * the serial version UID
33  	 */
34  	private static final long serialVersionUID = 1L;
35  
36  	/**
37  	 *
38  	 */
39  	public ToolException() {
40  		this(null, null, false);
41  	}
42  
43  	/**
44  	 * @param message
45  	 *            the exception message
46  	 */
47  	public ToolException(String message) {
48  		this(message, null, false);
49  	}
50  
51  	/**
52  	 * @param message
53  	 *            the exception message
54  	 * @param result
55  	 *            the execution result
56  	 * @param commandExecutionError
57  	 *            is command execution error happened ?
58  	 */
59  	public ToolException(String message, ExecutionResult result,
60  			boolean commandExecutionError) {
61  		super(message);
62  		this.result = result;
63  		this.commandExecutionError = commandExecutionError;
64  	}
65  
66  	/**
67  	 * @param message
68  	 *            the exception message
69  	 * @param cause
70  	 *            the cause for throw
71  	 */
72  	public ToolException(String message, Throwable cause) {
73  		super(message, cause);
74  		result = null;
75  		commandExecutionError = false;
76  	}
77  
78  	/**
79  	 * @param cause
80  	 *            the cause for throw
81  	 */
82  	public ToolException(Throwable cause) {
83  		super(cause);
84  		result = null;
85  		commandExecutionError = false;
86  	}
87  
88  	/**
89  	 * @return true if result is valid, false else
90  	 */
91  	public boolean isResult() {
92  		return result != null;
93  	}
94  
95  	/**
96  	 * @return the execution result
97  	 */
98  	public ExecutionResult getResult() {
99  		return result;
100 	}
101 
102 	/**
103 	 * @return true if command execution error appears, false otherwise
104 	 */
105 	public boolean isCommandExecutionError() {
106 		return commandExecutionError;
107 	}
108 
109 	/**
110 	 * @return the result Stderr
111 	 */
112 	public String getResultStderr() {
113 		if (result == null) {
114 			return ""; //$NON-NLS-1$
115 		}
116 		try {
117 			return new String(result.getStderr().toByteArray());
118 		} catch (Exception e) {
119 			LOG.warn("Failed to retrieve standard error output", e); //$NON-NLS-1$
120 		}
121 		return ""; //$NON-NLS-1$
122 	}
123 
124 	/**
125 	 * @return the result Stdout
126 	 */
127 	public String getResultStdout() {
128 		if (result == null) {
129 			return ""; //$NON-NLS-1$
130 		}
131 		try {
132 			return new String(result.getStdout().toByteArray());
133 		} catch (Exception e) {
134 			LOG.warn("Failed to retrieve standard output", e); //$NON-NLS-1$
135 		}
136 		return ""; //$NON-NLS-1$
137 	}
138 
139 }