From: Julien Lancelot Date: Fri, 13 Dec 2013 10:00:46 +0000 (+0100) Subject: Rename HttpException to ServerException, and remove ServerErrorException (managed... X-Git-Tag: 4.2~990 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=5c9c1d754a814bffea825e17e8489d0c045cd6cb;p=sonarqube.git Rename HttpException to ServerException, and remove ServerErrorException (managed error 500) --- diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java index 6fc45d5e9ae..669338302fd 100644 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java +++ b/sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java @@ -24,7 +24,7 @@ import javax.annotation.Nullable; /** * Request is not valid and can not be processed. */ -public class BadRequestException extends HttpException { +public class BadRequestException extends ServerException { private static final int BAD_REQUEST = 400; diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/ForbiddenException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/ForbiddenException.java index 262cf249395..17436746637 100644 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/ForbiddenException.java +++ b/sonar-server/src/main/java/org/sonar/server/exceptions/ForbiddenException.java @@ -22,7 +22,7 @@ package org.sonar.server.exceptions; /** * Permission denied. User does not have the required permissions. */ -public class ForbiddenException extends HttpException { +public class ForbiddenException extends ServerException { private static final int FORBIDDEN = 403; diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/HttpException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/HttpException.java deleted file mode 100644 index 7a8cd4176a8..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/HttpException.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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.server.exceptions; - -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; - -import java.util.Collection; -import java.util.Collections; - -import static com.google.common.collect.Lists.newArrayList; - -public class HttpException extends RuntimeException { - private final int httpCode; - - private final String l10nKey; - private final Collection l10nParams; - - public HttpException(int httpCode) { - this.httpCode = httpCode; - this.l10nKey = null; - this.l10nParams = null; - } - - public HttpException(int httpCode, String message) { - super(message); - this.httpCode = httpCode; - this.l10nKey = null; - this.l10nParams = null; - } - - public HttpException(int httpCode, @Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams) { - super(message); - this.httpCode = httpCode; - this.l10nKey = l10nKey; - this.l10nParams = l10nParams == null ? Collections.emptyList() : Collections.unmodifiableCollection(newArrayList(l10nParams)); - } - - public int httpCode() { - return httpCode; - } - - @CheckForNull - public String l10nKey() { - return l10nKey; - } - - @CheckForNull - public Collection l10nParams() { - return l10nParams; - } -} diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java index d39aa061e5f..27a6486e165 100644 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java +++ b/sonar-server/src/main/java/org/sonar/server/exceptions/NotFoundException.java @@ -19,7 +19,7 @@ */ package org.sonar.server.exceptions; -public class NotFoundException extends HttpException { +public class NotFoundException extends ServerException { private static final int NOT_FOUND = 404; diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/ServerErrorException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/ServerErrorException.java deleted file mode 100644 index c5110bf8c44..00000000000 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/ServerErrorException.java +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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.server.exceptions; - -public class ServerErrorException extends HttpException { - - private static final int SERVER_ERROR = 500; - - public ServerErrorException(String message) { - super(SERVER_ERROR, message); - } -} diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java new file mode 100644 index 00000000000..8687f7f35b0 --- /dev/null +++ b/sonar-server/src/main/java/org/sonar/server/exceptions/ServerException.java @@ -0,0 +1,69 @@ +/* + * 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.server.exceptions; + +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +import java.util.Collection; +import java.util.Collections; + +import static com.google.common.collect.Lists.newArrayList; + +public class ServerException extends RuntimeException { + private final int httpCode; + + private final String l10nKey; + private final Collection l10nParams; + + public ServerException(int httpCode) { + this.httpCode = httpCode; + this.l10nKey = null; + this.l10nParams = null; + } + + public ServerException(int httpCode, String message) { + super(message); + this.httpCode = httpCode; + this.l10nKey = null; + this.l10nParams = null; + } + + public ServerException(int httpCode, @Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams) { + super(message); + this.httpCode = httpCode; + this.l10nKey = l10nKey; + this.l10nParams = l10nParams == null ? Collections.emptyList() : Collections.unmodifiableCollection(newArrayList(l10nParams)); + } + + public int httpCode() { + return httpCode; + } + + @CheckForNull + public String l10nKey() { + return l10nKey; + } + + @CheckForNull + public Collection l10nParams() { + return l10nParams; + } +} diff --git a/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java b/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java index 4dd6cedf35b..ba552c49c52 100644 --- a/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java +++ b/sonar-server/src/main/java/org/sonar/server/exceptions/UnauthorizedException.java @@ -22,7 +22,7 @@ package org.sonar.server.exceptions; /** * User needs to be authenticated. HTTP request is generally redirected to login form. */ -public class UnauthorizedException extends HttpException { +public class UnauthorizedException extends ServerException { private static final int UNAUTHORIZED = 401; diff --git a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java index 5c6b8ba3069..2b481d4c45c 100644 --- a/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java +++ b/sonar-server/src/main/java/org/sonar/server/permission/InternalPermissionTemplateService.java @@ -29,10 +29,10 @@ import org.sonar.core.permission.PermissionTemplateDao; import org.sonar.core.permission.PermissionTemplateDto; import org.sonar.core.user.UserDao; import org.sonar.server.exceptions.BadRequestException; -import org.sonar.server.exceptions.ServerErrorException; import javax.annotation.CheckForNull; import javax.annotation.Nullable; + import java.util.List; import java.util.Map; import java.util.regex.Pattern; @@ -91,11 +91,6 @@ public class InternalPermissionTemplateService implements ServerComponent { validateTemplateName(null, name); validateKeyPattern(keyPattern); PermissionTemplateDto permissionTemplateDto = permissionTemplateDao.createPermissionTemplate(name, description, keyPattern); - if (permissionTemplateDto.getId() == null) { - String errorMsg = "Template creation failed"; - LOG.error(errorMsg); - throw new ServerErrorException(errorMsg); - } return PermissionTemplate.create(permissionTemplateDto); } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb index b605def0a17..e07c0b62e00 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/application_controller.rb @@ -196,7 +196,7 @@ class ApplicationController < ActionController::Base render_native_access_denied(error) elsif error.java_kind_of? Java::OrgSonarServerExceptions::ForbiddenException render_native_access_denied(error) - elsif error.java_kind_of? Java::OrgSonarServerExceptions::HttpException + elsif error.java_kind_of? Java::OrgSonarServerExceptions::ServerException render_server_exception(error) else render_error(error) diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb index 4b1cc9026e6..3e888a7c35e 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/users_controller.rb @@ -166,7 +166,7 @@ class UsersController < ApplicationController Api.users.deactivate(user.login) flash[:notice] = 'User is deleted.' rescue NativeException => exception - if exception.cause.java_kind_of? Java::OrgSonarServerExceptions::HttpException + if exception.cause.java_kind_of? Java::OrgSonarServerExceptions::ServerException error = exception.cause flash[:error] = (error.getMessage ? error.getMessage : Api::Utils.message(error.l10nKey, :params => error.l10nParams.to_a)) else diff --git a/sonar-server/src/test/java/org/sonar/server/exceptions/HttpExceptionTest.java b/sonar-server/src/test/java/org/sonar/server/exceptions/HttpExceptionTest.java deleted file mode 100644 index 7b6619bab65..00000000000 --- a/sonar-server/src/test/java/org/sonar/server/exceptions/HttpExceptionTest.java +++ /dev/null @@ -1,57 +0,0 @@ -/* - * 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.server.exceptions; - -import org.junit.Test; - -import static org.fest.assertions.Assertions.assertThat; - -public class HttpExceptionTest { - - @Test - public void should_create_exception_with_status(){ - HttpException exception = new HttpException(400); - assertThat(exception.httpCode()).isEqualTo(400); - } - - @Test - public void should_create_exception_with_status_and_message(){ - HttpException exception = new HttpException(404, "Not found"); - assertThat(exception.httpCode()).isEqualTo(404); - assertThat(exception.getMessage()).isEqualTo("Not found"); - } - - @Test - public void should_create_exception_with_status_and_l10n_message_with_param(){ - HttpException exception = new HttpException(404, null, "key", new String[]{"value"}); - assertThat(exception.httpCode()).isEqualTo(404); - assertThat(exception.l10nKey()).isEqualTo("key"); - assertThat(exception.l10nParams()).containsOnly("value"); - } - - @Test - public void should_create_exception_with_status_and_l10n_message_without_param(){ - HttpException exception = new HttpException(404, null, "key", null); - assertThat(exception.httpCode()).isEqualTo(404); - assertThat(exception.l10nKey()).isEqualTo("key"); - assertThat(exception.l10nParams()).isEmpty(); - } -} diff --git a/sonar-server/src/test/java/org/sonar/server/exceptions/ServerExceptionTest.java b/sonar-server/src/test/java/org/sonar/server/exceptions/ServerExceptionTest.java new file mode 100644 index 00000000000..42acd196d7a --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/exceptions/ServerExceptionTest.java @@ -0,0 +1,57 @@ +/* + * 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.server.exceptions; + +import org.junit.Test; + +import static org.fest.assertions.Assertions.assertThat; + +public class ServerExceptionTest { + + @Test + public void should_create_exception_with_status(){ + ServerException exception = new ServerException(400); + assertThat(exception.httpCode()).isEqualTo(400); + } + + @Test + public void should_create_exception_with_status_and_message(){ + ServerException exception = new ServerException(404, "Not found"); + assertThat(exception.httpCode()).isEqualTo(404); + assertThat(exception.getMessage()).isEqualTo("Not found"); + } + + @Test + public void should_create_exception_with_status_and_l10n_message_with_param(){ + ServerException exception = new ServerException(404, null, "key", new String[]{"value"}); + assertThat(exception.httpCode()).isEqualTo(404); + assertThat(exception.l10nKey()).isEqualTo("key"); + assertThat(exception.l10nParams()).containsOnly("value"); + } + + @Test + public void should_create_exception_with_status_and_l10n_message_without_param(){ + ServerException exception = new ServerException(404, null, "key", null); + assertThat(exception.httpCode()).isEqualTo(404); + assertThat(exception.l10nKey()).isEqualTo("key"); + assertThat(exception.l10nParams()).isEmpty(); + } +}