X-Git-Url: http://juplo.de/gitweb/?a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fkafka%2Fchat%2Fbackend%2Fapi%2FChatBackendControllerAdvice.java;h=90b639ff550d99a8a616d04e2886ce8ca08418d9;hb=df207aa9a8cd349fd43785270d250a7f55593801;hp=b64f7e001159f23a5b8a0a67fb48d66ff3014ff2;hpb=f2f0cdc6901d1117ac385e10e2c8a28a1886726c;p=demos%2Fkafka%2Fchat diff --git a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java index b64f7e00..90b639ff 100644 --- a/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java +++ b/src/main/java/de/juplo/kafka/chat/backend/api/ChatBackendControllerAdvice.java @@ -1,6 +1,9 @@ package de.juplo.kafka.chat.backend.api; -import de.juplo.kafka.chat.backend.domain.MessageMutationException; +import de.juplo.kafka.chat.backend.domain.exceptions.InvalidUsernameException; +import de.juplo.kafka.chat.backend.domain.exceptions.MessageMutationException; +import de.juplo.kafka.chat.backend.domain.exceptions.ShardNotOwnedException; +import de.juplo.kafka.chat.backend.domain.exceptions.UnknownChatroomException; import org.springframework.beans.factory.annotation.Value; import org.springframework.http.HttpStatus; import org.springframework.http.ProblemDetail; @@ -44,6 +47,38 @@ public class ChatBackendControllerAdvice problem.setDetail(stringBuilder.toString()); problem.setProperty("chatroomId", e.getChatroomId()); + problem.setProperty("shard", e.getShard()); + problem.setProperty("ownedShards", e.getOwnedShards()); + + return problem; + } + + @ExceptionHandler(ShardNotOwnedException.class) + public final ProblemDetail handleException( + ShardNotOwnedException e, + ServerWebExchange exchange, + UriComponentsBuilder uriComponentsBuilder) + { + final HttpStatus status = HttpStatus.NOT_FOUND; + ProblemDetail problem = ProblemDetail.forStatus(status); + + problem.setProperty("timestamp", new Date()); + + problem.setProperty("requestId", exchange.getRequest().getId()); + + problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/shard-not-owned").build().toUri()); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(status.getReasonPhrase()); + stringBuilder.append(" - "); + stringBuilder.append(e.getMessage()); + problem.setTitle(stringBuilder.toString()); + + stringBuilder.setLength(0); + stringBuilder.append("Shard not owned: "); + stringBuilder.append(e.getShard()); + problem.setDetail(stringBuilder.toString()); + + problem.setProperty("shard", e.getShard()); return problem; } @@ -76,9 +111,42 @@ public class ChatBackendControllerAdvice stringBuilder.append(" cannot be mutated!"); problem.setDetail(stringBuilder.toString()); - problem.setProperty("mutatedMessage", e.getMutated()); + problem.setProperty("existingMessage", MessageTo.from(e.getExisting())); + + problem.setProperty("mutatedText", e.getMutatedText()); + + return problem; + } + + @ExceptionHandler(InvalidUsernameException.class) + public final ProblemDetail handleException( + InvalidUsernameException e, + ServerWebExchange exchange, + UriComponentsBuilder uriComponentsBuilder) + { + final HttpStatus status = HttpStatus.BAD_REQUEST; + ProblemDetail problem = ProblemDetail.forStatus(status); + + problem.setProperty("timestamp", new Date()); + + problem.setProperty("requestId", exchange.getRequest().getId()); + + problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/invalid-username").build().toUri()); + StringBuilder stringBuilder = new StringBuilder(); + stringBuilder.append(status.getReasonPhrase()); + stringBuilder.append(" - "); + stringBuilder.append(e.getMessage()); + problem.setTitle(stringBuilder.toString()); + + stringBuilder.setLength(0); + stringBuilder.append("Invalid username: "); + stringBuilder.append(e.getUsername()); + stringBuilder.append( + "! A valid username must consist of at at least two letters and " + + "must only contain lower case letters a-z, numbers and dashes"); + problem.setDetail(stringBuilder.toString()); - problem.setProperty("existingMessage", e.getExisting()); + problem.setProperty("username", e.getUsername()); return problem; }