ToolException.java

  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. package org.eclipse.jgit.internal.diffmergetool;

  11. import org.eclipse.jgit.util.FS.ExecutionResult;
  12. import org.eclipse.jgit.util.SystemReader;
  13. import org.slf4j.Logger;
  14. import org.slf4j.LoggerFactory;

  15. /**
  16.  * Tool exception for differentiation.
  17.  *
  18.  */
  19. public class ToolException extends Exception {

  20.     private final static Logger LOG = LoggerFactory
  21.             .getLogger(ToolException.class);

  22.     private final ExecutionResult result;

  23.     private final boolean commandExecutionError;

  24.     /**
  25.      * the serial version UID
  26.      */
  27.     private static final long serialVersionUID = 1L;

  28.     /**
  29.      *
  30.      */
  31.     public ToolException() {
  32.         this(null, null, false);
  33.     }

  34.     /**
  35.      * @param message
  36.      *            the exception message
  37.      */
  38.     public ToolException(String message) {
  39.         this(message, null, false);
  40.     }

  41.     /**
  42.      * @param message
  43.      *            the exception message
  44.      * @param result
  45.      *            the execution result
  46.      * @param commandExecutionError
  47.      *            is command execution error happened ?
  48.      */
  49.     public ToolException(String message, ExecutionResult result,
  50.             boolean commandExecutionError) {
  51.         super(message);
  52.         this.result = result;
  53.         this.commandExecutionError = commandExecutionError;
  54.     }

  55.     /**
  56.      * @param message
  57.      *            the exception message
  58.      * @param cause
  59.      *            the cause for throw
  60.      */
  61.     public ToolException(String message, Throwable cause) {
  62.         super(message, cause);
  63.         result = null;
  64.         commandExecutionError = false;
  65.     }

  66.     /**
  67.      * @param cause
  68.      *            the cause for throw
  69.      */
  70.     public ToolException(Throwable cause) {
  71.         super(cause);
  72.         result = null;
  73.         commandExecutionError = false;
  74.     }

  75.     /**
  76.      * @return true if result is valid, false else
  77.      */
  78.     public boolean isResult() {
  79.         return result != null;
  80.     }

  81.     /**
  82.      * @return the execution result
  83.      */
  84.     public ExecutionResult getResult() {
  85.         return result;
  86.     }

  87.     /**
  88.      * @return true if command execution error appears, false otherwise
  89.      */
  90.     public boolean isCommandExecutionError() {
  91.         return commandExecutionError;
  92.     }

  93.     /**
  94.      * @return the result Stderr
  95.      */
  96.     public String getResultStderr() {
  97.         if (result == null) {
  98.             return ""; //$NON-NLS-1$
  99.         }
  100.         try {
  101.             return new String(result.getStderr().toByteArray(),
  102.                     SystemReader.getInstance().getDefaultCharset());
  103.         } catch (Exception e) {
  104.             LOG.warn("Failed to retrieve standard error output", e); //$NON-NLS-1$
  105.         }
  106.         return ""; //$NON-NLS-1$
  107.     }

  108.     /**
  109.      * @return the result Stdout
  110.      */
  111.     public String getResultStdout() {
  112.         if (result == null) {
  113.             return ""; //$NON-NLS-1$
  114.         }
  115.         try {
  116.             return new String(result.getStdout().toByteArray(),
  117.                     SystemReader.getInstance().getDefaultCharset());
  118.         } catch (Exception e) {
  119.             LOG.warn("Failed to retrieve standard output", e); //$NON-NLS-1$
  120.         }
  121.         return ""; //$NON-NLS-1$
  122.     }

  123. }