Commit 8b70cbdb551bc3cded37ed7532cfab7a3f495967

Authored by AndrewVolosytnykhThingsboard
Committed by Andrew Shvayka
1 parent 7c201213

Handling exceptions to exclude output like [object Object]

... ... @@ -26,8 +26,10 @@ import org.springframework.security.authentication.DisabledException;
26 26 import org.springframework.security.authentication.LockedException;
27 27 import org.springframework.security.core.AuthenticationException;
28 28 import org.springframework.security.web.access.AccessDeniedHandler;
29   -import org.springframework.stereotype.Component;
  29 +import org.springframework.web.bind.annotation.ExceptionHandler;
  30 +import org.springframework.web.bind.annotation.RestControllerAdvice;
30 31 import org.springframework.web.client.HttpClientErrorException;
  32 +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
31 33 import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
32 34 import org.thingsboard.server.common.data.exception.ThingsboardException;
33 35 import org.thingsboard.server.common.msg.tools.TbRateLimitsException;
... ... @@ -40,14 +42,15 @@ import javax.servlet.http.HttpServletRequest;
40 42 import javax.servlet.http.HttpServletResponse;
41 43 import java.io.IOException;
42 44
43   -@Component
44 45 @Slf4j
45   -public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
  46 +@RestControllerAdvice
  47 +public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler {
46 48
47 49 @Autowired
48 50 private ObjectMapper mapper;
49 51
50 52 @Override
  53 + @ExceptionHandler(AccessDeniedException.class)
51 54 public void handle(HttpServletRequest request, HttpServletResponse response,
52 55 AccessDeniedException accessDeniedException) throws IOException,
53 56 ServletException {
... ... @@ -60,6 +63,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
60 63 }
61 64 }
62 65
  66 + @ExceptionHandler(Exception.class)
63 67 public void handle(Exception exception, HttpServletResponse response) {
64 68 log.debug("Processing exception {}", exception.getMessage(), exception);
65 69 if (!response.isCommitted()) {
... ... @@ -159,7 +163,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler {
159 163 } else if (authenticationException instanceof AuthMethodNotSupportedException) {
160 164 mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED));
161 165 } else if (authenticationException instanceof UserPasswordExpiredException) {
162   - UserPasswordExpiredException expiredException = (UserPasswordExpiredException)authenticationException;
  166 + UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException;
163 167 String resetToken = expiredException.getResetToken();
164 168 mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken));
165 169 } else {
... ...