From 133402c1cab8e4ac38e3dec1a52bbc8bd6b73a14 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 17 Feb 2016 12:55:49 +0300 Subject: [PATCH] #EA-79158 Report code fragment when translator throws an exception. It should help to get a code sample for this issues and all similar issues. --- .../TranslationRuntimeException.java | 28 +++++++++++++++++++ .../PackageDeclarationTranslator.java | 14 +++++++++- .../js/translate/general/Translation.java | 18 ++++++++---- 3 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 js/js.translator/src/org/jetbrains/kotlin/js/facade/exceptions/TranslationRuntimeException.java diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/facade/exceptions/TranslationRuntimeException.java b/js/js.translator/src/org/jetbrains/kotlin/js/facade/exceptions/TranslationRuntimeException.java new file mode 100644 index 00000000000..b5ab8cae018 --- /dev/null +++ b/js/js.translator/src/org/jetbrains/kotlin/js/facade/exceptions/TranslationRuntimeException.java @@ -0,0 +1,28 @@ +/* + * 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. + */ + +package org.jetbrains.kotlin.js.facade.exceptions; + +import com.intellij.psi.PsiElement; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt; + +public class TranslationRuntimeException extends RuntimeException { + public TranslationRuntimeException(@NotNull PsiElement element, @Nullable Throwable cause) { + super("Unexpected error occurred compiling the following fragment: " + PsiUtilsKt.getTextWithLocation(element), cause); + } +} diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/PackageDeclarationTranslator.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/PackageDeclarationTranslator.java index fbb4d2388e9..18babaafe12 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/PackageDeclarationTranslator.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/declaration/PackageDeclarationTranslator.java @@ -20,6 +20,7 @@ import com.google.dart.compiler.backend.js.ast.*; import gnu.trove.THashMap; import org.jetbrains.annotations.NotNull; import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor; +import org.jetbrains.kotlin.js.facade.exceptions.TranslationRuntimeException; import org.jetbrains.kotlin.js.translate.context.Namer; import org.jetbrains.kotlin.js.translate.context.TranslationContext; import org.jetbrains.kotlin.js.translate.general.AbstractTranslator; @@ -63,7 +64,18 @@ public final class PackageDeclarationTranslator extends AbstractTranslator { packageFragmentToTranslator.put(packageFragment, translator); } - translator.translate(file); + try { + translator.translate(file); + } + catch (TranslationRuntimeException e) { + throw e; + } + catch (RuntimeException e) { + throw new TranslationRuntimeException(file, e); + } + catch (AssertionError e) { + throw new TranslationRuntimeException(file, e); + } } for (PackageTranslator translator : packageFragmentToTranslator.values()) { diff --git a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java index 2d0b53c9cc3..96312de3732 100644 --- a/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java +++ b/js/js.translator/src/org/jetbrains/kotlin/js/translate/general/Translation.java @@ -24,10 +24,7 @@ import org.jetbrains.kotlin.descriptors.ModuleDescriptor; import org.jetbrains.kotlin.idea.MainFunctionDetector; import org.jetbrains.kotlin.js.config.Config; import org.jetbrains.kotlin.js.facade.MainCallParameters; -import org.jetbrains.kotlin.js.facade.exceptions.MainFunctionNotFoundException; -import org.jetbrains.kotlin.js.facade.exceptions.TranslationException; -import org.jetbrains.kotlin.js.facade.exceptions.TranslationInternalException; -import org.jetbrains.kotlin.js.facade.exceptions.UnsupportedFeatureException; +import org.jetbrains.kotlin.js.facade.exceptions.*; import org.jetbrains.kotlin.js.translate.callTranslator.CallTranslator; import org.jetbrains.kotlin.js.translate.context.Namer; import org.jetbrains.kotlin.js.translate.context.StaticContext; @@ -105,7 +102,18 @@ public final class Translation { @NotNull private static JsNode doTranslateExpression(KtExpression expression, TranslationContext context) { - return expression.accept(new ExpressionVisitor(), context); + try { + return expression.accept(new ExpressionVisitor(), context); + } + catch (TranslationRuntimeException e) { + throw e; + } + catch (RuntimeException e) { + throw new TranslationRuntimeException(expression, e); + } + catch (AssertionError e) { + throw new TranslationRuntimeException(expression, e); + } } @NotNull