Browse Source

SONAR-4383 Improve errors handling

tags/3.7
Julien Lancelot 11 years ago
parent
commit
da1e8934e2

+ 12
- 2
sonar-server/src/main/java/org/sonar/server/exceptions/BadRequestException.java View File

@@ -19,6 +19,8 @@
*/
package org.sonar.server.exceptions;

import javax.annotation.Nullable;

/**
* Request is not valid and can not be processed.
*/
@@ -30,8 +32,16 @@ public class BadRequestException extends HttpException {
super(BAD_REQUEST, message);
}

public BadRequestException(String l10nKey, Object... l10nParams) {
super(BAD_REQUEST, l10nKey, l10nParams);
public BadRequestException(@Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams) {
super(BAD_REQUEST, message, l10nKey, l10nParams);
}

public static BadRequestException of(String message) {
return new BadRequestException(message);
}

public static BadRequestException ofL10n(String l10nKey, Object... l10nParams) {
return new BadRequestException(null, l10nKey, l10nParams);
}

}

+ 3
- 1
sonar-server/src/main/java/org/sonar/server/exceptions/HttpException.java View File

@@ -20,6 +20,7 @@
package org.sonar.server.exceptions;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

public class HttpException extends RuntimeException {
private final int httpCode;
@@ -36,7 +37,8 @@ public class HttpException extends RuntimeException {
this.httpCode = httpCode;
}

public HttpException(int httpCode, String l10nKey, Object... l10nParams) {
public HttpException(int httpCode, @Nullable String message, @Nullable String l10nKey, @Nullable Object[] l10nParams) {
super(message);
this.httpCode = httpCode;
this.l10nKey = l10nKey;
this.l10nParams = l10nParams;

+ 3
- 3
sonar-server/src/main/java/org/sonar/server/issue/InternalRubyIssueService.java View File

@@ -584,20 +584,20 @@ public class InternalRubyIssueService implements ServerComponent {

private void checkMandatoryParameter(String value, String paramName) {
if (Strings.isNullOrEmpty(value)) {
throw new BadRequestException("errors.cant_be_empty", paramName);
throw BadRequestException.ofL10n("errors.cant_be_empty", paramName);
}
}

private void checkMandatorySizeParameter(String value, String paramName, Integer size) {
checkMandatoryParameter(value, paramName);
if (!Strings.isNullOrEmpty(value) && value.length() > size) {
throw new BadRequestException("errors.is_too_long", paramName, size);
throw BadRequestException.ofL10n("errors.is_too_long", paramName, size);
}
}

private void checkOptionalSizeParameter(String value, String paramName, Integer size) {
if (!Strings.isNullOrEmpty(value) && value.length() > size) {
throw new BadRequestException("errors.is_too_long", paramName, size);
throw BadRequestException.ofL10n("errors.is_too_long", paramName, size);
}
}


Loading…
Cancel
Save