From 5819bd5d7d0cbe13cd2ceda0650032f8009ef0d4 Mon Sep 17 00:00:00 2001 From: Klaudio Sinani Date: Mon, 8 Nov 2021 17:56:58 +0100 Subject: SONAR-15579 Improve error verbosity on permission template matching key collision + unit test --- .../exceptions/TemplateMatchingKeyException.java | 51 ++++++++++++++++++++++ .../TemplateMatchingKeyExceptionTest.java | 35 +++++++++++++++ 2 files changed, 86 insertions(+) create mode 100644 server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/TemplateMatchingKeyException.java create mode 100644 server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/TemplateMatchingKeyExceptionTest.java (limited to 'server/sonar-webserver-api') diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/TemplateMatchingKeyException.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/TemplateMatchingKeyException.java new file mode 100644 index 00000000000..b296dd6f282 --- /dev/null +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/exceptions/TemplateMatchingKeyException.java @@ -0,0 +1,51 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.server.exceptions; + + import com.google.common.base.MoreObjects; + import java.util.List; + + import static java.net.HttpURLConnection.HTTP_BAD_REQUEST; + import static java.util.Collections.singletonList; + +/** + * Provided request is not valid due to Permission template matching key collision. + */ +public class TemplateMatchingKeyException extends ServerException { + private final transient List errors; + + public TemplateMatchingKeyException(String errorMessage) { + super(HTTP_BAD_REQUEST, errorMessage); + this.errors = singletonList(errorMessage); + } + + public List errors() { + return this.errors; + } + + @Override + public String toString() { + return MoreObjects.toStringHelper(this) + .add("errors", this.errors()) + .toString(); + } + +} + diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/TemplateMatchingKeyExceptionTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/TemplateMatchingKeyExceptionTest.java new file mode 100644 index 00000000000..e68355a8839 --- /dev/null +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/exceptions/TemplateMatchingKeyExceptionTest.java @@ -0,0 +1,35 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program 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. + * + * This program 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.server.exceptions; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class TemplateMatchingKeyExceptionTest { + + @Test + public void testMethods() { + TemplateMatchingKeyException ex = new TemplateMatchingKeyException("error"); + + assertThat(ex).hasToString("TemplateMatchingKeyException{errors=[error]}"); + } + +} -- cgit v1.2.3