TSK-1690: Removed toString() methods from Exceptions to prevent unspecific error messages

This commit is contained in:
Martin Sawilla 2021-09-29 09:32:13 +02:00 committed by Martin Sawilla
parent 1bab31e43c
commit b511973c14
14 changed files with 68 additions and 60 deletions

View File

@ -11,9 +11,4 @@ public class AutocommitFailedException extends TaskanaRuntimeException {
public AutocommitFailedException(Throwable cause) {
super("Autocommit failed", ErrorCode.of(ERROR_KEY), cause);
}
@Override
public String toString() {
return "AutocommitFailedException{}";
}
}

View File

@ -23,9 +23,4 @@ public class ConcurrencyException extends TaskanaException {
public String getEntityId() {
return entityId;
}
@Override
public String toString() {
return "ConcurrencyException{}";
}
}

View File

@ -12,9 +12,4 @@ public class ConnectionNotSetException extends TaskanaRuntimeException {
public ConnectionNotSetException() {
super("Connection not set", ErrorCode.of(ERROR_KEY));
}
@Override
public String toString() {
return "ConnectionNotSetException{}";
}
}

View File

@ -18,9 +18,4 @@ public class DomainNotFoundException extends NotFoundException {
public String getDomain() {
return domain;
}
@Override
public String toString() {
return "DomainNotFoundException [domain=" + domain + "]";
}
}

View File

@ -12,9 +12,4 @@ public class InvalidArgumentException extends TaskanaException {
public InvalidArgumentException(String msg, Throwable cause) {
super(msg, ErrorCode.of(ERROR_KEY), cause);
}
@Override
public String toString() {
return "InvalidArgumentException{}";
}
}

View File

@ -33,13 +33,4 @@ public class MismatchedRoleException extends NotAuthorizedException {
public String getCurrentUserId() {
return currentUserId;
}
@Override
public String toString() {
return "MismatchedRoleException [currentUserId="
+ currentUserId
+ ", roles="
+ Arrays.toString(roles)
+ "]";
}
}

View File

@ -6,9 +6,4 @@ public class NotAuthorizedException extends TaskanaException {
protected NotAuthorizedException(String msg, ErrorCode errorCode) {
super(msg, errorCode);
}
@Override
public String toString() {
return "NotAuthorizedException{}";
}
}

View File

@ -6,9 +6,4 @@ public class NotFoundException extends TaskanaException {
protected NotFoundException(String message, ErrorCode errorCode) {
super(message, errorCode);
}
@Override
public String toString() {
return "NotFoundException{}";
}
}

View File

@ -13,8 +13,4 @@ public class SystemException extends TaskanaRuntimeException {
super(msg, ErrorCode.of(ERROR_KEY), cause);
}
@Override
public String toString() {
return "SystemException{}";
}
}

View File

@ -20,6 +20,6 @@ public class TaskanaException extends Exception {
@Override
public String toString() {
return "TaskanaException [errorCode=" + errorCode + "]";
return "TaskanaException [errorCode=" + errorCode + ", message=" + getMessage() + "]";
}
}

View File

@ -20,6 +20,6 @@ public class TaskanaRuntimeException extends RuntimeException {
@Override
public String toString() {
return "TaskanaRuntimeException [errorCode=" + errorCode + "]";
return "TaskanaRuntimeException [errorCode=" + errorCode + ", message=" + getMessage() + "]";
}
}

View File

@ -20,9 +20,4 @@ public class UnsupportedDatabaseException extends TaskanaRuntimeException {
public String getDatabaseProductName() {
return databaseProductName;
}
@Override
public String toString() {
return "UnsupportedDatabaseException [databaseProductName=" + databaseProductName + "]";
}
}

View File

@ -22,9 +22,4 @@ public class WrongCustomHolidayFormatException extends TaskanaException {
public String getCustomHoliday() {
return customHoliday;
}
@Override
public String toString() {
return "WrongCustomHolidayFormatException [customHoliday=" + customHoliday + "]";
}
}

View File

@ -20,6 +20,7 @@ import com.tngtech.archunit.lang.ArchCondition;
import com.tngtech.archunit.lang.ArchRule;
import com.tngtech.archunit.lang.ConditionEvents;
import com.tngtech.archunit.lang.SimpleConditionEvent;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@ -153,6 +154,34 @@ class ArchitectureTest {
myRule.check(importedClasses);
}
@Test
void exceptionsThatShouldNotHaveToStringMethod() {
ArchRule myRule =
classes()
.that()
.areAssignableTo(TaskanaException.class)
.or()
.areAssignableTo(TaskanaRuntimeException.class)
.and()
.doNotBelongToAnyOf(TaskanaRuntimeException.class, TaskanaException.class)
.should(notImplementToString());
myRule.check(importedClasses);
}
@Test
void exceptionsThatShouldHaveToStringMethod() {
ArchRule myRule =
classes()
.that()
.areAssignableFrom(TaskanaRuntimeException.class)
.or()
.areAssignableFrom(TaskanaException.class)
.should(implementToString());
myRule.check(importedClasses);
}
@Test
void onlyExceptionsShouldResideInExceptionPackage() {
ArchRule myRule =
@ -320,6 +349,43 @@ class ArchitectureTest {
rule.check(importedClasses);
}
private ArchCondition<JavaClass> implementToString() {
return new ArchCondition<JavaClass>("implement toString()") {
@Override
public void check(JavaClass javaClass, ConditionEvents conditionEvents) {
boolean implementToString =
Arrays.stream(javaClass.reflect().getDeclaredMethods())
.map(Method::getName)
.anyMatch("toString"::equals);
if (!implementToString) {
conditionEvents.add(
SimpleConditionEvent.violated(
javaClass,
String.format(
"Class '%s' does not implement toString()", javaClass.getFullName())));
}
}
};
}
private ArchCondition<JavaClass> notImplementToString() {
return new ArchCondition<JavaClass>("not implement toString()") {
@Override
public void check(JavaClass javaClass, ConditionEvents conditionEvents) {
boolean implementToString =
Arrays.stream(javaClass.reflect().getDeclaredMethods())
.map(Method::getName)
.anyMatch("toString"::equals);
if (implementToString) {
conditionEvents.add(
SimpleConditionEvent.violated(
javaClass,
String.format("Class '%s' does implement toString()", javaClass.getFullName())));
}
}
};
}
private ArchCondition<JavaClass> beAnnotatedWithTestInstancePerClass() {
return new ArchCondition<JavaClass>("be annotated with @TestInstance(Lifecycle.PER_CLASS)") {
@Override