1 package de.juplo.kafka.chat.backend.api;
3 import de.juplo.kafka.chat.backend.domain.InvalidUsernameException;
4 import de.juplo.kafka.chat.backend.domain.MessageMutationException;
5 import de.juplo.kafka.chat.backend.domain.UnknownChatroomException;
6 import org.springframework.beans.factory.annotation.Value;
7 import org.springframework.http.HttpStatus;
8 import org.springframework.http.ProblemDetail;
9 import org.springframework.web.bind.annotation.ControllerAdvice;
10 import org.springframework.web.bind.annotation.ExceptionHandler;
11 import org.springframework.web.server.ServerWebExchange;
12 import org.springframework.web.util.UriComponentsBuilder;
14 import java.util.Date;
18 public class ChatBackendControllerAdvice
20 @Value("${server.context-path:/}")
23 @ExceptionHandler(UnknownChatroomException.class)
24 public final ProblemDetail handleException(
25 UnknownChatroomException e,
26 ServerWebExchange exchange,
27 UriComponentsBuilder uriComponentsBuilder)
29 final HttpStatus status = HttpStatus.NOT_FOUND;
30 ProblemDetail problem = ProblemDetail.forStatus(status);
32 problem.setProperty("timestamp", new Date());
34 problem.setProperty("requestId", exchange.getRequest().getId());
36 problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/unknown-chatroom").build().toUri());
37 StringBuilder stringBuilder = new StringBuilder();
38 stringBuilder.append(status.getReasonPhrase());
39 stringBuilder.append(" - ");
40 stringBuilder.append(e.getMessage());
41 problem.setTitle(stringBuilder.toString());
43 stringBuilder.setLength(0);
44 stringBuilder.append("Chatroom unknown: ");
45 stringBuilder.append(e.getChatroomId());
46 problem.setDetail(stringBuilder.toString());
48 problem.setProperty("chatroomId", e.getChatroomId());
53 @ExceptionHandler(MessageMutationException.class)
54 public final ProblemDetail handleException(
55 MessageMutationException e,
56 ServerWebExchange exchange,
57 UriComponentsBuilder uriComponentsBuilder)
59 final HttpStatus status = HttpStatus.BAD_REQUEST;
60 ProblemDetail problem = ProblemDetail.forStatus(status);
62 problem.setProperty("timestamp", new Date());
64 problem.setProperty("requestId", exchange.getRequest().getId());
66 problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/message-mutation").build().toUri());
67 StringBuilder stringBuilder = new StringBuilder();
68 stringBuilder.append(status.getReasonPhrase());
69 stringBuilder.append(" - ");
70 stringBuilder.append(e.getMessage());
71 problem.setTitle(stringBuilder.toString());
73 stringBuilder.setLength(0);
74 stringBuilder.append("The existing message with user=");
75 stringBuilder.append(e.getExisting().getUsername());
76 stringBuilder.append(" and id=");
77 stringBuilder.append(e.getExisting().getId());
78 stringBuilder.append(" cannot be mutated!");
79 problem.setDetail(stringBuilder.toString());
81 problem.setProperty("existingMessage", MessageTo.from(e.getExisting()));
83 problem.setProperty("mutatedText", e.getMutatedText());
88 @ExceptionHandler(InvalidUsernameException.class)
89 public final ProblemDetail handleException(
90 InvalidUsernameException e,
91 ServerWebExchange exchange,
92 UriComponentsBuilder uriComponentsBuilder)
94 final HttpStatus status = HttpStatus.BAD_REQUEST;
95 ProblemDetail problem = ProblemDetail.forStatus(status);
97 problem.setProperty("timestamp", new Date());
99 problem.setProperty("requestId", exchange.getRequest().getId());
101 problem.setType(uriComponentsBuilder.replacePath(contextPath).path("/problem/invalid-username").build().toUri());
102 StringBuilder stringBuilder = new StringBuilder();
103 stringBuilder.append(status.getReasonPhrase());
104 stringBuilder.append(" - ");
105 stringBuilder.append(e.getMessage());
106 problem.setTitle(stringBuilder.toString());
108 stringBuilder.setLength(0);
109 stringBuilder.append("Invalid username: ");
110 stringBuilder.append(e.getUsername());
111 stringBuilder.append(
112 "! A valid username must consist of at at least two letters and " +
113 "must only contain lower case letters a-z, numbers and dashes");
114 problem.setDetail(stringBuilder.toString());
116 problem.setProperty("username", e.getUsername());