001 /* 002 * Copyright 2010-2013 JetBrains s.r.o. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017 package org.jetbrains.jet.codegen; 018 019 import com.intellij.openapi.application.ApplicationManager; 020 import com.intellij.openapi.util.Computable; 021 import com.intellij.psi.PsiElement; 022 import org.jetbrains.annotations.NotNull; 023 import org.jetbrains.annotations.Nullable; 024 import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; 025 026 public class CompilationException extends RuntimeException { 027 private final PsiElement element; 028 029 public CompilationException(@NotNull String message, @Nullable Throwable cause, @NotNull PsiElement element) { 030 super(message, cause); 031 this.element = element; 032 } 033 034 @NotNull 035 public PsiElement getElement() { 036 return element; 037 } 038 039 040 private String where() { 041 Throwable cause = getCause(); 042 Throwable throwable = cause != null ? cause : this; 043 StackTraceElement[] stackTrace = throwable.getStackTrace(); 044 if (stackTrace != null && stackTrace.length > 0) { 045 return stackTrace[0].getFileName() + ":" + stackTrace[0].getLineNumber(); 046 } 047 return "unknown"; 048 } 049 050 @Override 051 public String getMessage() { 052 return ApplicationManager.getApplication().runReadAction(new Computable<String>() { 053 @Override 054 public String compute() { 055 StringBuilder message = 056 new StringBuilder("Back-end (JVM) Internal error: ").append(CompilationException.super.getMessage()).append("\n"); 057 Throwable cause = getCause(); 058 if (cause != null) { 059 String causeMessage = cause.getMessage(); 060 message.append("Cause: ").append(causeMessage == null ? cause.toString() : causeMessage).append("\n"); 061 } 062 message.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n"); 063 message.append("PsiElement: ").append(element.getText()).append("\n"); 064 message.append("The root cause was thrown at: ").append(where()); 065 066 return message.toString(); 067 } 068 }); 069 } 070 }