001 /* 002 * SonarQube, open source software quality management tool. 003 * Copyright (C) 2008-2014 SonarSource 004 * mailto:contact AT sonarsource DOT com 005 * 006 * SonarQube is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 3 of the License, or (at your option) any later version. 010 * 011 * SonarQube is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public License 017 * along with this program; if not, write to the Free Software Foundation, 018 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 019 */ 020 package org.sonar.api.utils; 021 022 import javax.annotation.CheckForNull; 023 import javax.annotation.Nullable; 024 025 import java.util.Collection; 026 import java.util.Collections; 027 028 import static com.google.common.collect.Lists.newArrayList; 029 030 /** 031 * Runtime exception for "functional" error. It aims to be displayed to end-users, without any technical information 032 * like stack traces. It requires sonar-runner 2.4. Previous versions log stack trace. 033 * <p/> 034 * Note that by design Maven still logs the stack trace when the option -e is set. 035 * <p/> 036 * Message should be clear and complete. Keep in mind that context is not added to the exception. 037 * Names of processed resource and decorator are for example not automatically added when throwing {@link MessageException} 038 * from {@link org.sonar.api.batch.Decorator}. 039 * 040 * @since 3.7.1 041 */ 042 public class MessageException extends RuntimeException { 043 044 private final String l10nKey; 045 private final Collection<Object> l10nParams; 046 047 private MessageException(String s) { 048 this(s, null, null); 049 } 050 051 private MessageException(@Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams) { 052 super(message); 053 this.l10nKey = l10nKey; 054 this.l10nParams = l10nParams == null ? Collections.emptyList() : Collections.unmodifiableCollection(newArrayList(l10nParams)); 055 } 056 057 public static MessageException of(String message) { 058 return new MessageException(message); 059 } 060 061 public static MessageException ofL10n(String l10nKey, Object... l10nParams) { 062 return new MessageException(null, l10nKey, l10nParams); 063 } 064 065 /** 066 * Does not fill in the stack trace 067 * 068 * @see java.lang.Throwable#fillInStackTrace() 069 */ 070 @Override 071 public synchronized Throwable fillInStackTrace() { 072 return this; 073 } 074 075 @Override 076 public String toString() { 077 return getMessage(); 078 } 079 080 @CheckForNull 081 public String l10nKey() { 082 return l10nKey; 083 } 084 085 @CheckForNull 086 public Collection<Object> l10nParams() { 087 return l10nParams; 088 } 089 090 }