001/* 002 * SonarQube 003 * Copyright (C) 2009-2017 SonarSource SA 004 * mailto:info AT sonarsource DOT com 005 * 006 * This program 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 * This program 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 */ 020package org.sonar.api.utils; 021 022import java.util.Collection; 023import javax.annotation.CheckForNull; 024import javax.annotation.Nullable; 025 026import static java.util.Arrays.asList; 027import static java.util.Collections.emptyList; 028 029/** 030 * Runtime exception for "functional" error. It aims to be displayed to end-users, without any technical information 031 * like stack traces. 032 * <br> 033 * 034 * It's handling depends on the versions of the sonar-batch and sonar-runner. sonar-runner 2.4 will only show the 035 * message associated with this exception. 036 * Starting from sonar-batch 5.3, this is handled in the batch side, and the main goal is to hide all wrappers of this 037 * exception. If this exception is created without cause, then only the message associated with this exception is shown; 038 * otherwise, its causes are also shown. 039 * Previous combinations of sonar-batch/sonar-runner log all stack trace. 040 * <br> 041 * Message should be clear and complete. Keep in mind that context might not be added to the exception. 042 * Names of processed resource and decorator are for example not automatically added when throwing {@link MessageException} 043 * from {@link org.sonar.api.batch.Decorator}. 044 * 045 * @since 3.7.1 046 */ 047public class MessageException extends RuntimeException { 048 049 private final String l10nKey; 050 private final Collection<Object> l10nParams; 051 052 private MessageException(String s) { 053 this(s, null, null); 054 } 055 056 private MessageException(@Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams) { 057 super(message); 058 this.l10nKey = l10nKey; 059 this.l10nParams = l10nParams == null ? emptyList() : asList(l10nParams); 060 } 061 062 private MessageException(String message, Throwable cause) { 063 super(message, cause); 064 l10nKey = null; 065 l10nParams = emptyList(); 066 } 067 068 public static MessageException of(String message, Throwable cause) { 069 return new MessageException(message, cause); 070 } 071 072 public static MessageException of(String message) { 073 return new MessageException(message); 074 } 075 076 public static MessageException ofL10n(String l10nKey, Object... l10nParams) { 077 return new MessageException(null, l10nKey, l10nParams); 078 } 079 080 /** 081 * Does not fill in the stack trace 082 * 083 * @see java.lang.Throwable#fillInStackTrace() 084 */ 085 @Override 086 public synchronized Throwable fillInStackTrace() { 087 return this; 088 } 089 090 @Override 091 public String toString() { 092 return getMessage(); 093 } 094 095 @CheckForNull 096 public String l10nKey() { 097 return l10nKey; 098 } 099 100 @CheckForNull 101 public Collection<Object> l10nParams() { 102 return l10nParams; 103 } 104 105}