diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-08-13 17:48:50 +0200 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-08-13 17:49:02 +0200 |
commit | d5ca3028e37cd6861d1eff6a2cd2e5609f8fdfd1 (patch) | |
tree | 9d7c02e2590395b85f67fc8850e30b4440c81b7b /sonar-plugin-api/src | |
parent | ec103daf26445aa08b7a22cb2a9e367efa0f37d4 (diff) | |
download | sonarqube-d5ca3028e37cd6861d1eff6a2cd2e5609f8fdfd1.tar.gz sonarqube-d5ca3028e37cd6861d1eff6a2cd2e5609f8fdfd1.zip |
SONAR-4547 API: new class "MessageException" to stop a pending process and to log an error message without any stack trace
Diffstat (limited to 'sonar-plugin-api/src')
-rw-r--r-- | sonar-plugin-api/src/main/java/org/sonar/api/utils/MessageException.java | 50 | ||||
-rw-r--r-- | sonar-plugin-api/src/test/java/org/sonar/api/utils/MessageExceptionTest.java | 50 |
2 files changed, 100 insertions, 0 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/MessageException.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/MessageException.java new file mode 100644 index 00000000000..25290dac9b2 --- /dev/null +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/MessageException.java @@ -0,0 +1,50 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.utils; + +/** + * Runtime exception for "functional" errors. It aims to be displayed to end-users, without any technical information + * like stack traces. + * + * @since 4.0 + */ +public class MessageException extends RuntimeException { + + public MessageException(String s) { + super(s); + } + + /** + * Does not fill in the stack trace + * + * @see java.lang.Throwable#fillInStackTrace() + */ + @Override + public synchronized Throwable fillInStackTrace() { + return this; + } + + @Override + public String toString() { + return getMessage(); + } + + +} diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/utils/MessageExceptionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/utils/MessageExceptionTest.java new file mode 100644 index 00000000000..0b734416e94 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/utils/MessageExceptionTest.java @@ -0,0 +1,50 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2013 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * SonarQube is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.api.utils; + +import org.junit.Test; + +import java.io.PrintWriter; +import java.io.StringWriter; + +import static org.fest.assertions.Assertions.assertThat; + +public class MessageExceptionTest { + + /** + * The exception should log only the message, without the "org.sonar.api.utils.MessageException" prefix + * and stack traces + */ + @Test + public void should_not_print_stacktrace() throws Exception { + String message = "the message"; + try { + throw new MessageException(message); + + } catch (MessageException e) { + StringWriter writer = new StringWriter(); + e.printStackTrace(new PrintWriter(writer)); + + assertThat(e.getStackTrace()).isEmpty(); + assertThat(e.getMessage()).isEqualTo(message); + assertThat(writer.toString()).isEqualTo(message + System.getProperty("line.separator")); + } + } +} |