WIP:exception
authorKai Moritz <kai@juplo.de>
Sun, 26 Feb 2023 14:17:21 +0000 (15:17 +0100)
committerKai Moritz <kai@juplo.de>
Sun, 26 Feb 2023 14:35:29 +0000 (15:35 +0100)
src/main/java/de/juplo/kafka/chat/backend/domain/ShardNotOwnedException.java [new file with mode: 0644]
src/main/java/de/juplo/kafka/chat/backend/domain/UnknownChatroomException.java

diff --git a/src/main/java/de/juplo/kafka/chat/backend/domain/ShardNotOwnedException.java b/src/main/java/de/juplo/kafka/chat/backend/domain/ShardNotOwnedException.java
new file mode 100644 (file)
index 0000000..d467eab
--- /dev/null
@@ -0,0 +1,68 @@
+package de.juplo.kafka.chat.backend.domain;
+
+import lombok.Getter;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.stream.Collectors;
+
+
+public class ShardNotOwnedException extends IllegalStateException
+{
+  @Getter
+  private final ChatHomeService chatHomeService;
+  @Getter
+  private final ChatRoomInfo chatRoomInfo;
+  @Getter
+  private final int shard;
+  @Getter
+  private final int[] ownedShards;
+
+
+  public ShardNotOwnedException(
+      ChatHomeService chatHomeService,
+      ChatRoomInfo chatRoomInfo,
+      int shard,
+      Collection<Integer> ownedShards)
+  {
+    this(
+        chatHomeService,
+        chatRoomInfo,
+        shard,
+        ShardNotOwnedException.toArray(ownedShards));
+  }
+
+  public ShardNotOwnedException(
+      ChatHomeService chatHomeService,
+      ChatRoomInfo chatRoomInfo,
+      int shard,
+      int[] ownedShards)
+  {
+    super(
+        chatHomeService +
+        " does not own the shard " +
+        shard +
+        " for ChatRoom " +
+        chatRoomInfo +
+        " owned shards: " +
+        Arrays
+            .stream(ownedShards)
+            .mapToObj(ownedShard -> Integer.toString(ownedShard))
+            .collect(Collectors.joining(", ")));
+    this.chatHomeService = chatHomeService;
+    this.chatRoomInfo = chatRoomInfo;
+    this.shard = shard;
+    this.ownedShards = ownedShards;
+  }
+
+
+  private static int[] toArray(Collection<Integer> collection)
+  {
+    int[] array = new int[collection.size()];
+    Iterator<Integer> iterator = collection.iterator();
+    for (int i = 0; iterator.hasNext(); i++)
+      array[i] = iterator.next();
+    return array;
+  }
+}
index 1f70f11..714c220 100644 (file)
@@ -2,17 +2,43 @@ package de.juplo.kafka.chat.backend.domain;
 
 import lombok.Getter;
 
+import java.util.Arrays;
+import java.util.Optional;
 import java.util.UUID;
+import java.util.stream.Collectors;
 
 
-public class UnknownChatroomException extends RuntimeException
+public class UnknownChatroomException extends IllegalStateException
 {
   @Getter
   private final UUID chatroomId;
+  @Getter
+  private final Optional<Integer> shard;
+  @Getter
+  private final Optional<int[]> ownedShards;
 
   public UnknownChatroomException(UUID chatroomId)
   {
     super("Chatroom does not exist: " + chatroomId);
     this.chatroomId = chatroomId;
+    this.shard = Optional.empty();
+    this.ownedShards = Optional.empty();
+  }
+
+  public UnknownChatroomException(UUID chatroomId, int shard, int[] ownedShards)
+  {
+    super(
+        "Chatroom does not exist (here): " +
+        chatroomId +
+        " shard=" +
+        shard +
+        ", owned=" +
+        Arrays
+            .stream(ownedShards)
+            .mapToObj(ownedShard -> Integer.toString(ownedShard))
+            .collect(Collectors.joining(",")));
+    this.chatroomId = chatroomId;
+    this.shard = Optional.of(shard);
+    this.ownedShards = Optional.of(ownedShards);
   }
 }