Commit 8b70cbdb551bc3cded37ed7532cfab7a3f495967
Committed by
Andrew Shvayka
1 parent
7c201213
Handling exceptions to exclude output like [object Object]
Showing
1 changed file
with
8 additions
and
4 deletions
@@ -26,8 +26,10 @@ import org.springframework.security.authentication.DisabledException; | @@ -26,8 +26,10 @@ import org.springframework.security.authentication.DisabledException; | ||
26 | import org.springframework.security.authentication.LockedException; | 26 | import org.springframework.security.authentication.LockedException; |
27 | import org.springframework.security.core.AuthenticationException; | 27 | import org.springframework.security.core.AuthenticationException; |
28 | import org.springframework.security.web.access.AccessDeniedHandler; | 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 | import org.springframework.web.client.HttpClientErrorException; | 31 | import org.springframework.web.client.HttpClientErrorException; |
32 | +import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler; | ||
31 | import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; | 33 | import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; |
32 | import org.thingsboard.server.common.data.exception.ThingsboardException; | 34 | import org.thingsboard.server.common.data.exception.ThingsboardException; |
33 | import org.thingsboard.server.common.msg.tools.TbRateLimitsException; | 35 | import org.thingsboard.server.common.msg.tools.TbRateLimitsException; |
@@ -40,14 +42,15 @@ import javax.servlet.http.HttpServletRequest; | @@ -40,14 +42,15 @@ import javax.servlet.http.HttpServletRequest; | ||
40 | import javax.servlet.http.HttpServletResponse; | 42 | import javax.servlet.http.HttpServletResponse; |
41 | import java.io.IOException; | 43 | import java.io.IOException; |
42 | 44 | ||
43 | -@Component | ||
44 | @Slf4j | 45 | @Slf4j |
45 | -public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { | 46 | +@RestControllerAdvice |
47 | +public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler { | ||
46 | 48 | ||
47 | @Autowired | 49 | @Autowired |
48 | private ObjectMapper mapper; | 50 | private ObjectMapper mapper; |
49 | 51 | ||
50 | @Override | 52 | @Override |
53 | + @ExceptionHandler(AccessDeniedException.class) | ||
51 | public void handle(HttpServletRequest request, HttpServletResponse response, | 54 | public void handle(HttpServletRequest request, HttpServletResponse response, |
52 | AccessDeniedException accessDeniedException) throws IOException, | 55 | AccessDeniedException accessDeniedException) throws IOException, |
53 | ServletException { | 56 | ServletException { |
@@ -60,6 +63,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { | @@ -60,6 +63,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { | ||
60 | } | 63 | } |
61 | } | 64 | } |
62 | 65 | ||
66 | + @ExceptionHandler(Exception.class) | ||
63 | public void handle(Exception exception, HttpServletResponse response) { | 67 | public void handle(Exception exception, HttpServletResponse response) { |
64 | log.debug("Processing exception {}", exception.getMessage(), exception); | 68 | log.debug("Processing exception {}", exception.getMessage(), exception); |
65 | if (!response.isCommitted()) { | 69 | if (!response.isCommitted()) { |
@@ -159,7 +163,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { | @@ -159,7 +163,7 @@ public class ThingsboardErrorResponseHandler implements AccessDeniedHandler { | ||
159 | } else if (authenticationException instanceof AuthMethodNotSupportedException) { | 163 | } else if (authenticationException instanceof AuthMethodNotSupportedException) { |
160 | mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); | 164 | mapper.writeValue(response.getWriter(), ThingsboardErrorResponse.of(authenticationException.getMessage(), ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED)); |
161 | } else if (authenticationException instanceof UserPasswordExpiredException) { | 165 | } else if (authenticationException instanceof UserPasswordExpiredException) { |
162 | - UserPasswordExpiredException expiredException = (UserPasswordExpiredException)authenticationException; | 166 | + UserPasswordExpiredException expiredException = (UserPasswordExpiredException) authenticationException; |
163 | String resetToken = expiredException.getResetToken(); | 167 | String resetToken = expiredException.getResetToken(); |
164 | mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken)); | 168 | mapper.writeValue(response.getWriter(), ThingsboardCredentialsExpiredResponse.of(expiredException.getMessage(), resetToken)); |
165 | } else { | 169 | } else { |