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 017package org.jetbrains.k2js.translate.utils; 018 019import com.google.common.collect.Lists; 020import com.google.dart.compiler.backend.js.ast.JsExpression; 021import com.google.dart.compiler.common.SourceInfo; 022import com.intellij.psi.PsiElement; 023import org.jetbrains.annotations.NotNull; 024import org.jetbrains.annotations.Nullable; 025import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor; 026import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; 027import org.jetbrains.jet.lang.psi.JetExpression; 028import org.jetbrains.jet.lang.resolve.BindingContext; 029 030import java.util.List; 031 032public 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}