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