/*
 * Copyright 2000-2013 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.intellij.openapi.project;

import com.intellij.openapi.util.Computable;
import org.jetbrains.annotations.Nullable;

import java.io.PrintStream;
import java.io.PrintWriter;

/**
 * Thrown on accessing indices when they're not ready, in so-called dumb mode. Possible fixes:
 * <ul>
 * <li> If {@link com.intellij.openapi.actionSystem.AnAction#actionPerformed(com.intellij.openapi.actionSystem.AnActionEvent)} is in stack trace,
 * consider making the action not implement {@link DumbAware}.
 *
 * <li> A {@link DumbAware} action, having got this exception, may just notify the user that the requested activity is not possible while
 * indexing is in progress. It can be done via a dialog (see {@link com.intellij.openapi.ui.Messages}) or a status bar balloon
 * (see {@link DumbService#showDumbModeNotification(String)}, {@link com.intellij.openapi.actionSystem.ex.ActionUtil#showDumbModeWarning(com.intellij.openapi.actionSystem.AnActionEvent...)}).
 *
 * <li> If index access is performed from some non-urgent invokeLater activity, consider replacing it with
 * {@link DumbService#smartInvokeLater(Runnable)}. Note that this 'later' can be very late, several minutes may pass. So if that code
 * involves user interaction, {@link DumbService#smartInvokeLater(Runnable)} should probably not be used to avoid dialogs popping out of the blue.
 *
 * <li> If it's a non-urgent background process (e.g. compilation, usage search), consider replacing topmost read-action with
 * {@link DumbService#runReadActionInSmartMode(Computable)}.
 *
 * <li> If the exception comes from within Java's findClass call, and the IDE is currently performing a user-initiated action or a
 * task when skipping findClass would lead to very negative consequences (e.g. not stopping at a breakpoint), then it might be possible
 * to avoid index query by using alternative resolve (and findClass) strategy, which is significantly slower and might return null. To do this,
 * use {@link DumbService#setAlternativeResolveEnabled(boolean)}.
 *
 * <li> It's preferable to avoid the exception entirely by adding {@link DumbService#isDumb()} checks where necessary.
 * </ul>
 *
 * @author peter
 * @see DumbService
 * @see DumbAware
 */
public class IndexNotReadyException extends RuntimeException {
  @Nullable private final Throwable myStartTrace;

  public IndexNotReadyException() {
    this(null);
  }

  public IndexNotReadyException(@Nullable Throwable startTrace) {
    super("Please change caller according to " + IndexNotReadyException.class.getName() + " documentation");
    myStartTrace = startTrace;
  }

  @Override
  public void printStackTrace(PrintStream s) {
    super.printStackTrace(s);
    if (myStartTrace != null) {
      s.println();
      s.println("-----------");
      s.println("Indexing started at:");
      myStartTrace.printStackTrace(s);
    }
  }

  @Override
  public void printStackTrace(PrintWriter s) {
    super.printStackTrace(s);
    if (myStartTrace != null) {
      s.println();
      s.println("-----------");
      s.println("Indexing started at:");
      myStartTrace.printStackTrace(s);
    }
  }
}

@@ -665,123 +665,264 @@
 api.
-util.Computable;%0Aimport org.jetbrains.annotations.Null
+diagnostic.Attachment;%0Aimport com.intellij.openapi.diagnostic.ExceptionWithAttachments;%0Aimport com.intellij.openapi.util.Comput
 able;%0A
-%0A
 import 
-java.io.PrintStream;%0Aimport java.io.PrintWriter
+com.intellij.util.ExceptionUtil;%0Aimport org.jetbrains.annotations.NotNull;%0Aimport org.jetbrains.annotations.Nullable
 ;%0A%0A/
@@ -3011,16 +3011,52 @@
 ception 
+implements ExceptionWithAttachments 
 %7B%0A  @Nul
@@ -3364,24 +3364,35 @@
 Trace;%0A  %7D%0A%0A
+  @NotNull%0A
   @Override%0A
@@ -3404,84 +3404,51 @@
 lic 
-void printStackTrace(PrintStream s) %7B%0A    super.printStackTrace(s);%0A    if (
+Attachment%5B%5D getAttachments() %7B%0A    return 
 mySt
@@ -3460,418 +3460,167 @@
 ace 
-!
+=
 = null
-) %7B
 %0A      
-s.println();%0A      s.println(%22-----------%22);%0A      s.println(%22Indexing started at:%22);%0A      myStartTrace.printStackTrace(s);%0A    %7D%0A  %7D%0A%0A  @Override%0A  public void printStackTrace(PrintWriter s) %7B%0A    super.printStackTrace(s);%0A    if (my
+     ? Attachment.EMPTY_ARRAY%0A           : new Attachment%5B%5D%7Bnew Attachment(%22indexing
 Start
-T
+.t
 race
- != null) %7B%0A      s.println();%0A      s.println(%22-----------%22);%0A      s.println(%22Indexing started at:%22);%0A      myStartTrace.printStackTrace(s);%0A    %7D
+%22, ExceptionUtil.getThrowableText(myStartTrace))%7D;
 %0A  %7D

include start trace as an attachment