Skip to content

Commit

Permalink
Readded exception data as separate structures
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-schnell committed Mar 10, 2024
1 parent 94abcc6 commit 01de8b2
Show file tree
Hide file tree
Showing 102 changed files with 7,432 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
import java.io.Serial;

/**
* An aggregate already exists when trying to create it.
* Base class for aggregate related exceptions.
*/
public abstract class AbstractAggregateException extends Exception {

@Serial
private static final long serialVersionUID = 1L;

private final EntityType type;
private final String type;

private final AggregateRootId id;
private final String id;

/**
* Constructor with all data.
* Constructor with strongly typed data.
*
* @param message Error message.
* @param type Type of the aggregate.
Expand All @@ -49,6 +49,24 @@ public AbstractAggregateException(@NotEmpty final String message,
Contract.requireArgNotEmpty("message", message);
Contract.requireArgNotNull("aggregateType", type);
Contract.requireArgNotNull("aggregateId", id);
this.type = type.asString();
this.id = id.asString();
}

/**
* Constructor with string data.
*
* @param message Error message.
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
*/
public AbstractAggregateException(@NotEmpty final String message,
@NotEmpty final String type,
@NotEmpty final String id) {
super(message);
Contract.requireArgNotEmpty("message", message);
Contract.requireArgNotNull("aggregateType", type);
Contract.requireArgNotNull("aggregateId", id);
this.type = type;
this.id = id;
}
Expand All @@ -59,7 +77,7 @@ public AbstractAggregateException(@NotEmpty final String message,
* @return Type.
*/
@NotNull
public final EntityType getType() {
public final String getType() {
return type;
}

Expand All @@ -69,7 +87,7 @@ public final EntityType getType() {
* @return Stream with version conflict.
*/
@NotNull
public final AggregateRootId getId() {
public final String getId() {
return id;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import java.io.Serial;

/**
* An aggregate already exists when trying to create it.
* Base class for version related aggregate exceptions.
*/
public abstract class AbstractVersionedAggregateException extends AbstractAggregateException {

Expand All @@ -33,7 +33,7 @@ public abstract class AbstractVersionedAggregateException extends AbstractAggreg
private final int version;

/**
* Constructor with all data.
* Constructor with strongly typed data.
*
* @param message Error message.
* @param type Type of the aggregate.
Expand All @@ -44,6 +44,21 @@ public AbstractVersionedAggregateException(@NotEmpty final String message,
@NotNull final EntityType type,
@NotNull final AggregateRootId id,
final int version) {
this(message, type.asString(), id.asString(), version);
}

/**
* Constructor with string data.
*
* @param message Error message.
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
* @param version Version of the aggregate.
*/
public AbstractVersionedAggregateException(@NotEmpty final String message,
@NotNull final String type,
@NotNull final String id,
final int version) {
super(message, type, id);
this.version = version;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@
import static org.fuin.ddd4j.core.Ddd4JUtils.SHORT_ID_PREFIX;

/**
* An aggregate already exists when trying to create it.
* An aggregate already existed when trying to create it.
*/
public final class AggregateAlreadyExistsException extends AbstractVersionedAggregateException implements ExceptionShortIdentifable {

/**
* Unique name of the element to use for XML and JSON marshalling/unmarshalling.
*/
public static final String ELEMENT_NAME = "aggregate-already-exists-exception";

@Serial
private static final long serialVersionUID = 1L;

Expand All @@ -38,14 +43,25 @@ public final class AggregateAlreadyExistsException extends AbstractVersionedAggr
public static final String SHORT_ID = SHORT_ID_PREFIX + "-AGGREGATE_ALREADY_EXISTS";

/**
* Constructor with all data.
* Constructor with typed data.
*
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
* @param version Actual version.
*/
public AggregateAlreadyExistsException(@NotNull final EntityType type, @NotNull final AggregateRootId id, final int version) {
super(type.asString() + " " + id.asString() + " already exists (version=" + version + ")", type, id, version);
this(type.asString(), id.asString(), version);
}

/**
* Constructor with string data.
*
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
* @param version Actual version.
*/
public AggregateAlreadyExistsException(@NotNull final String type, @NotNull final String id, final int version) {
super(type + " " + id + " already exists (version=" + version + ")", type, id, version);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
*/
public final class AggregateDeletedException extends AbstractAggregateException implements ExceptionShortIdentifable {

/**
* Unique name of the element to use for XML and JSON marshalling/unmarshalling.
*/
public static final String ELEMENT_NAME = "aggregate-deleted-exception";

@Serial
private static final long serialVersionUID = 1L;

Expand All @@ -47,6 +52,16 @@ public AggregateDeletedException(@NotNull final EntityType type, @NotNull final
super(type.asString() + " with id " + id.asString() + " already deleted", type, id);
}

/**
* Constructor with string data.
*
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
*/
public AggregateDeletedException(@NotNull final String type, @NotNull final String id) {
super(type + " with id " + id + " already deleted", type, id);
}

@Override
public final String getShortId() {
return SHORT_ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
*/
public final class AggregateNotFoundException extends AbstractAggregateException implements ExceptionShortIdentifable {

/**
* Unique name of the element to use for XML and JSON marshalling/unmarshalling.
*/
public static final String ELEMENT_NAME = "aggregate-not-found-exception";

@Serial
private static final long serialVersionUID = 1L;

Expand All @@ -44,7 +49,17 @@ public final class AggregateNotFoundException extends AbstractAggregateException
* @param id Unique identifier of the aggregate.
*/
public AggregateNotFoundException(@NotNull final EntityType type, @NotNull final AggregateRootId id) {
super(type.asString() + " with id " + id.asString() + " not found", type, id);
this(type.asString(), id.asString());
}

/**
* Constructor with string data.
*
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
*/
public AggregateNotFoundException(@NotNull final String type, @NotNull final String id) {
super(type + " with id " + id + " not found", type, id);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
/**
* Signals a conflict between an expected and an actual version for an aggregate.
*/
public final class AggregateVersionConflictException extends AbstractVersionedAggregateException implements ExceptionShortIdentifable {
public final class AggregateVersionConflictException extends AbstractAggregateException implements ExceptionShortIdentifable {

/**
* Unique name of the element to use for XML and JSON marshalling/unmarshalling.
*/
public static final String ELEMENT_NAME = "aggregate-version-conflict-exception";

@Serial
private static final long serialVersionUID = 1L;
Expand All @@ -39,6 +44,8 @@ public final class AggregateVersionConflictException extends AbstractVersionedAg

private final int expected;

private final int actual;

/**
* Constructor with all data.
*
Expand All @@ -51,12 +58,28 @@ public AggregateVersionConflictException(@NotNull final EntityType type,
@NotNull final AggregateRootId id,
final int expected,
final int actual) {
super("Expected version " + expected + " for " + type.asString() + " (" + id.asString() + "), but was " + actual, type, id, actual);
this(type.asString(), id.asString(), expected, actual);
}

/**
* Constructor with string.
*
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
* @param expected Expected version.
* @param actual Actual version.
*/
public AggregateVersionConflictException(@NotNull final String type,
@NotNull final String id,
final int expected,
final int actual) {
super("Expected version " + expected + " for " + type + " (" + id + "), but was " + actual, type, id);
this.expected = expected;
this.actual = actual;
}

@Override
public final String getShortId() {
public String getShortId() {
return SHORT_ID;
}

Expand All @@ -65,7 +88,7 @@ public final String getShortId() {
*
* @return Expected version.
*/
public final int getExpected() {
public int getExpected() {
return expected;
}

Expand All @@ -74,8 +97,8 @@ public final int getExpected() {
*
* @return Actual version.
*/
public final int getActual() {
return getVersion();
public int getActual() {
return actual;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
*/
public final class AggregateVersionNotFoundException extends AbstractVersionedAggregateException implements ExceptionShortIdentifable {

/**
* Unique name of the element to use for XML and JSON marshalling/unmarshalling.
*/
public static final String ELEMENT_NAME = "aggregate-version-not-found-exception";

@Serial
private static final long serialVersionUID = 1L;

Expand All @@ -47,7 +52,20 @@ public final class AggregateVersionNotFoundException extends AbstractVersionedAg
public AggregateVersionNotFoundException(@NotNull final EntityType type,
@NotNull final AggregateRootId id,
final int version) {
super("Requested version " + version + " for " + type.asString() + " (" + id.asString() + ") does not exist", type, id, version);
this(type.asString(), id.asString(), version);
}

/**
* Constructor with string arguments.
*
* @param type Type of the aggregate.
* @param id Unique identifier of the aggregate.
* @param version Requested version.
*/
public AggregateVersionNotFoundException(@NotNull final String type,
@NotNull final String id,
final int version) {
super("Requested version " + version + " for " + type + " (" + id + ") does not exist", type, id, version);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
*/
public final class DecryptionFailedException extends Exception implements ExceptionShortIdentifable {

/**
* Unique name of the element to use for XML and JSON marshalling/unmarshalling.
*/
public static final String ELEMENT_NAME = "decryption-failed-exception";

@Serial
private static final long serialVersionUID = 1L;

Expand All @@ -42,7 +47,16 @@ public final class DecryptionFailedException extends Exception implements Except
* @param cause Original exception that caused the failure.
*/
public DecryptionFailedException(final Exception cause) {
super("Decryption failed", cause);
super("Decryption failed: " + cause.getMessage(), cause);
}

/**
* Constructor with message. Should only be used for re-creating the exception on the client side.
*
* @param msg Exception message.
*/
public DecryptionFailedException(final String msg) {
super(msg);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@
*/
public final class DuplicateEncryptionKeyIdException extends Exception implements ExceptionShortIdentifable {

/**
* Unique name of the element to use for XML and JSON marshalling/unmarshalling.
*/
public static final String ELEMENT_NAME = "duplicate-encryption-key-id-exception";

@Serial
private static final long serialVersionUID = 1L;

Expand Down
Loading

0 comments on commit 01de8b2

Please sign in to comment.