001    /*
002     * Copyright 2010-2015 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.kotlin.js.translate.utils;
018    
019    import com.intellij.psi.PsiElement;
020    import org.jetbrains.annotations.NotNull;
021    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
022    import org.jetbrains.kotlin.diagnostics.DiagnosticUtils;
023    import org.jetbrains.kotlin.psi.KtExpression;
024    
025    public final class ErrorReportingUtils {
026        private ErrorReportingUtils() {
027        }
028    
029        @NotNull
030        public static RuntimeException reportErrorWithLocation(@NotNull KtExpression selector, @NotNull RuntimeException e) {
031            return reportErrorWithLocation(e, DiagnosticUtils.atLocation(selector));
032        }
033    
034        @NotNull
035        private static RuntimeException reportErrorWithLocation(@NotNull RuntimeException e, @NotNull String location) {
036            throw new RuntimeException(e.getMessage() + " at " + location, e);
037        }
038    
039        @NotNull
040        public static String message(@NotNull PsiElement expression, @NotNull String messageText) {
041            return messageText + " at " + DiagnosticUtils.atLocation(expression) + ".";
042        }
043    
044        @NotNull
045        public static String message(@NotNull DeclarationDescriptor descriptor, @NotNull String explainingMessage) {
046            return explainingMessage + " at " + DiagnosticUtils.atLocation(descriptor) + ".";
047        }
048    
049        @NotNull
050        public static String message(@NotNull PsiElement element) {
051            return "Error at " + DiagnosticUtils.atLocation(element) + ".";
052        }
053    }