Package com.almworks.jira.structure.api

Source Code of com.almworks.jira.structure.api.StructureException

package com.almworks.jira.structure.api;

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.issue.MutableIssue;
import org.jetbrains.annotations.Nullable;

/**
* StructureException can be thrown for different causes that involve Structure plugin. It usually means that the
* operation that is traced back to the user's action, cannot be performed and should be reported as error.
*
* @see StructureError
* @author Igor Sereda
*/
public class StructureException extends Exception {
  // used by tests to avoid exceptions
  private static final boolean ISSUE_LOOKUP_DISABLED = "true".equalsIgnoreCase(System.getProperty("structure.exception.disable.lookup"));

  private final StructureError myError;
  private final long myStructure;
  private final long myIssue;

  /**
   * Constructs an instance of exception. For convenience, use one of the overloaded constructors.
   *
   * @param error structure error code
   * @param structure structure in question
   * @param issue related issue
   * @param message additional message text
   * @param cause throwable cause
   */
  public StructureException(StructureError error, @Nullable Long structure, @Nullable Long issue, @Nullable String message, @Nullable Throwable cause) {
    super(createMessage(message, error, structure, issue), cause);
    myError = error == null ? StructureError.GENERIC_ERROR : error;
    myStructure = structure == null ? 0L : structure;
    myIssue = issue == null ? 0L : issue;
  }

  private static String createMessage(String message, StructureError error, Long structure, Long issue) {
    StringBuilder b = new StringBuilder();
    if (error == null) error = StructureError.GENERIC_ERROR;
    if (message != null) b.append(message).append(": ");
    b.append("structure ").append(error.name()).append(" (code:").append(error.getCode());
    if (structure != null && structure > 0) b.append(" structure:").append(structure);
    if (issue != null && issue > 0) {
      b.append(" issue:").append(issue);
      if (!ISSUE_LOOKUP_DISABLED) {
        try {
          IssueManager issueManager = ComponentManager.getComponentInstanceOfType(IssueManager.class);
          MutableIssue io = issueManager.getIssueObject(issue);
          if (io != null) b.append(" key:").append(io.getKey());
        } catch (Exception e) {
          // ignore
        }
      }
    }
    b.append(')');
    return b.toString();
  }

  /**
   * Constructs an instance of exception.
   *
   * @param error structure error code
   */
  public StructureException(StructureError error) {
    this(error, null, null, null, null);
  }

  /**
   * Constructs an instance of exception.
   *
   * @param error structure error code
   * @param message additional message text
   */
  public StructureException(StructureError error, String message) {
    this(error, null, null, message, null);
  }

  /**
   * Constructs an instance of exception.
   *
   * @param error structure error code
   * @param structure structure in question
   * @param issue related issue
   */
  public StructureException(StructureError error, Long structure, Long issue) {
    this(error, structure, issue, null, null);
  }

  /**
   * Constructs an instance of exception.
   *
   * @param error structure error code
   * @param structure structure in question
   */
  public StructureException(StructureError error, Long structure) {
    this(error, structure, null, null, null);
  }

  /**
   * Constructs an instance of exception.
   *
   * @param error structure error code
   * @param structure structure in question
   * @param issue related issue
   * @param message additional message text
   */
  public StructureException(StructureError error, Long structure, Long issue, String message) {
    this(error, structure, issue, message, null);
  }

  /**
   * @return error code
   */
  public StructureError getError() {
    return myError;
  }

  /**
   * @return related structure, or 0 if no structure is related
   */
  public long getStructure() {
    return myStructure;
  }

  /**
   * @return related issue, or 0 if no issue is related
   */
  public long getIssue() {
    return myIssue;
  }
}
TOP

Related Classes of com.almworks.jira.structure.api.StructureException

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.