Send source code in exceptions as attachments, not text (common cases)

#KT-17678 In Progress
This commit is contained in:
Dmitry Jemerov
2017-12-29 12:42:11 +01:00
parent 817dadc120
commit 25ea53457e
10 changed files with 88 additions and 137 deletions
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2016 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.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.resolve.lazy
@@ -19,6 +8,7 @@ package org.jetbrains.kotlin.resolve.lazy
import org.jetbrains.kotlin.descriptors.DeclarationDescriptor
import org.jetbrains.kotlin.psi.KtDeclaration
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
interface AbsentDescriptorHandler {
fun diagnoseDescriptorNotFound(declaration: KtDeclaration): DeclarationDescriptor
@@ -29,5 +19,9 @@ class BasicAbsentDescriptorHandler : AbsentDescriptorHandler {
}
class NoDescriptorForDeclarationException @JvmOverloads constructor(declaration: KtDeclaration, additionalDetails: String? = null) :
IllegalStateException("Descriptor wasn't found for declaration $declaration\n${declaration.getElementTextWithContext()}"
+ (additionalDetails?.let { "\n---------------------------------------------------\n$it" } ?: ""))
KotlinExceptionWithAttachments("Descriptor wasn't found for declaration $declaration"
+ (additionalDetails?.let { "\n---------------------------------------------------\n$it" } ?: "")) {
init {
withAttachment("declaration.kt", declaration.getElementTextWithContext())
}
}
@@ -1,17 +1,6 @@
/*
* Copyright 2010-2016 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.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.types.expressions;
@@ -41,6 +30,7 @@ import org.jetbrains.kotlin.util.KotlinFrontEndException;
import org.jetbrains.kotlin.util.LookupTrackerUtilKt;
import org.jetbrains.kotlin.util.PerformanceCounter;
import org.jetbrains.kotlin.util.ReenteringLazyValueComputationException;
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments;
import static org.jetbrains.kotlin.diagnostics.Errors.TYPECHECKER_HAS_RUN_INTO_RECURSIVE_PROBLEM;
@@ -240,8 +230,8 @@ public abstract class ExpressionTypingVisitorDispatcher extends KtVisitor<Kotlin
try {
// This trows AssertionError in CLI and reports the error in the IDE
LOG.error(
"Exception while analyzing expression at " + DiagnosticUtils.atLocation(expression) + ":\n" + expression.getText() + "\n",
e
new KotlinExceptionWithAttachments("Exception while analyzing expression at " + DiagnosticUtils.atLocation(expression), e)
.withAttachment("expression.kt", expression.getText())
);
}
catch (AssertionError errorFromLogger) {
@@ -1,27 +1,20 @@
/*
* Copyright 2010-2016 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.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.util
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
import org.jetbrains.kotlin.utils.KotlinExceptionWithAttachments
class KotlinFrontEndException(message: String, cause: Throwable) : RuntimeException(message, cause) {
class KotlinFrontEndException(message: String, cause: Throwable) : KotlinExceptionWithAttachments(message, cause) {
constructor(
message: String,
cause: Throwable,
element: PsiElement
) : this(getExceptionMessage("Front-end", message, cause, element), cause)
) : this(getExceptionMessage("Front-end", message, cause, DiagnosticUtils.atLocation(element)), cause) {
withAttachment("element.kt", element.text)
}
}
@@ -1,30 +1,17 @@
/*
* Copyright 2010-2016 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.
* Copyright 2000-2017 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.util
import com.intellij.openapi.application.ApplicationManager
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
fun getExceptionMessage(
subsystemName: String,
message: String,
cause: Throwable?,
element: PsiElement?
location: String?
): String = ApplicationManager.getApplication().runReadAction<String> {
val result = StringBuilder(subsystemName + " Internal error: ").append(message).append("\n")
if (cause != null) {
@@ -32,9 +19,8 @@ fun getExceptionMessage(
result.append("Cause: ").append(causeMessage ?: cause.toString()).append("\n")
}
if (element != null) {
result.append("File being compiled and position: ").append(DiagnosticUtils.atLocation(element)).append("\n")
result.append("PsiElement: ").append(element.text).append("\n")
if (location != null) {
result.append("File being compiled and position: ").append(location).append("\n")
}
else {
result.append("Element is unknown")