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.k2js.translate.utils;
018
019 import com.google.common.collect.Lists;
020 import com.google.dart.compiler.backend.js.ast.JsExpression;
021 import com.google.dart.compiler.common.SourceInfo;
022 import com.intellij.psi.PsiElement;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
025 import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
026 import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
027 import org.jetbrains.jet.lang.psi.JetExpression;
028 import org.jetbrains.jet.lang.resolve.BindingContext;
029
030 import java.util.List;
031
032 public final class ErrorReportingUtils {
033 private ErrorReportingUtils() {
034 }
035
036 @NotNull
037 public static RuntimeException reportErrorWithLocation(@NotNull JetExpression selector, @NotNull RuntimeException e) {
038 return reportErrorWithLocation(e, DiagnosticUtils.atLocation(selector));
039 }
040
041 @NotNull
042 private static RuntimeException reportErrorWithLocation(@NotNull RuntimeException e, @NotNull String location) {
043 throw new RuntimeException(e.getMessage() + " at " + location, e);
044 }
045
046 @NotNull
047 public static String message(@NotNull PsiElement expression, @NotNull String messageText) {
048 return messageText + " at " + DiagnosticUtils.atLocation(expression) + ".";
049 }
050
051 @NotNull
052 public static String message(@NotNull BindingContext context,
053 @NotNull DeclarationDescriptor descriptor,
054 @NotNull String explainingMessage) {
055 return explainingMessage + " at " + DiagnosticUtils.atLocation(context, descriptor) + ".";
056 }
057
058 @NotNull
059 public static String message(@NotNull PsiElement element) {
060 return "Error at " + DiagnosticUtils.atLocation(element) + ".";
061 }
062
063 @NotNull
064 public static RuntimeException reportErrorWithLocation(@NotNull RuntimeException e,
065 @NotNull DeclarationDescriptor descriptor,
066 @NotNull BindingContext bindingContext) {
067 throw reportErrorWithLocation(e, DiagnosticUtils.atLocation(bindingContext, descriptor));
068 }
069
070 @NotNull
071 public static String atLocation(@Nullable JsExpression expression, @NotNull List<JsExpression> arguments) {
072 List<JsExpression> list = Lists.newArrayList(expression);
073 list.addAll(arguments);
074 for (JsExpression value : arguments) {
075 SourceInfo info = value.getSourceInfo();
076 if (info != null) {
077 return "at " + info.getSource().getUri() + " " + info.getLine() + ":" + info.getColumn();
078 }
079 }
080 return "at unknown location";
081 }
082 }