From 686e41bd393c221534bf2f969e541dcc13746554 Mon Sep 17 00:00:00 2001 From: dashevchenko Date: Wed, 6 Mar 2024 17:04:25 +0200 Subject: [PATCH] moved path not found error handing from ThingsboardErrorController to ThingsboardErrorResponseHandler --- .../ThingsboardErrorController.java | 56 ------------------- .../ThingsboardErrorResponseHandler.java | 33 ++++++++++- 2 files changed, 32 insertions(+), 57 deletions(-) delete mode 100644 application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java diff --git a/application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java b/application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java deleted file mode 100644 index b70edcba45..0000000000 --- a/application/src/main/java/org/thingsboard/server/controller/ThingsboardErrorController.java +++ /dev/null @@ -1,56 +0,0 @@ -/** - * Copyright © 2016-2024 The Thingsboard Authors - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.thingsboard.server.controller; - -import lombok.extern.slf4j.Slf4j; -import org.springframework.boot.web.servlet.error.ErrorController; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.ControllerAdvice; -import org.springframework.web.bind.annotation.RequestMapping; -import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; -import org.thingsboard.server.exception.ThingsboardErrorResponse; - -import javax.servlet.RequestDispatcher; -import javax.servlet.http.HttpServletRequest; - -@Slf4j -@ControllerAdvice -public class ThingsboardErrorController implements ErrorController { - - public static final String PATH_NOT_FOUND_ERROR_DESCRIPTION = "Path is not found."; - public static final String GENERAL_ERROR_DESCRIPTION = "Something went wrong! Our Engineers are on it"; - - @RequestMapping("/error") - public ResponseEntity handleError(HttpServletRequest request) { - Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); - - HttpHeaders httpHeaders = new HttpHeaders(); - httpHeaders.setContentType(MediaType.APPLICATION_JSON); - - if (status != null) { - int statusCode = Integer.parseInt(status.toString()); - - if(statusCode == HttpStatus.NOT_FOUND.value()) { - return new ResponseEntity<>(ThingsboardErrorResponse.of(PATH_NOT_FOUND_ERROR_DESCRIPTION, ThingsboardErrorCode.ITEM_NOT_FOUND, HttpStatus.NOT_FOUND), httpHeaders, HttpStatus.NOT_FOUND); - } - } - return new ResponseEntity<>(ThingsboardErrorResponse.of(GENERAL_ERROR_DESCRIPTION, ThingsboardErrorCode.GENERAL, HttpStatus.INTERNAL_SERVER_ERROR), httpHeaders, HttpStatus.INTERNAL_SERVER_ERROR); - } - -} diff --git a/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java b/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java index b2509187d5..ec149fede8 100644 --- a/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java +++ b/application/src/main/java/org/thingsboard/server/exception/ThingsboardErrorResponseHandler.java @@ -16,6 +16,7 @@ package org.thingsboard.server.exception; import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.web.servlet.error.ErrorController; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; @@ -28,7 +29,9 @@ import org.springframework.security.authentication.LockedException; import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.web.access.AccessDeniedHandler; +import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.context.request.WebRequest; @@ -43,6 +46,7 @@ import org.thingsboard.server.service.security.exception.JwtExpiredTokenExceptio import org.thingsboard.server.service.security.exception.UserPasswordExpiredException; import org.thingsboard.server.service.security.exception.UserPasswordNotValidException; +import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @@ -50,9 +54,15 @@ import java.io.IOException; import java.util.HashMap; import java.util.Map; +import static javax.servlet.RequestDispatcher.ERROR_EXCEPTION; + @Slf4j +@Controller @RestControllerAdvice -public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler { +public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler, ErrorController { + + public static final String PATH_NOT_FOUND_ERROR_DESCRIPTION = "Path is not found."; + public static final String GENERAL_ERROR_DESCRIPTION = "Something went wrong!"; private static final Map statusToErrorCodeMap = new HashMap<>(); static { @@ -90,6 +100,27 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand return errorCodeToStatusMap.getOrDefault(errorCode, HttpStatus.INTERNAL_SERVER_ERROR); } + @RequestMapping("/error") + public ResponseEntity handleError(HttpServletRequest request) { + Object status = request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE); + + if (status != null) { + int statusCode = Integer.parseInt(status.toString()); + if(statusCode == HttpStatus.NOT_FOUND.value()) { + return new ResponseEntity<>(ThingsboardErrorResponse.of(PATH_NOT_FOUND_ERROR_DESCRIPTION, ThingsboardErrorCode.ITEM_NOT_FOUND, HttpStatus.NOT_FOUND), HttpStatus.NOT_FOUND); + } + } + String errorMessage; + Throwable throwable = (Throwable) + request.getAttribute(ERROR_EXCEPTION); + if (throwable != null) { + errorMessage = throwable.getMessage(); + } else { + errorMessage = GENERAL_ERROR_DESCRIPTION; + } + return new ResponseEntity<>(ThingsboardErrorResponse.of(errorMessage, ThingsboardErrorCode.GENERAL, HttpStatus.INTERNAL_SERVER_ERROR), HttpStatus.INTERNAL_SERVER_ERROR); + } + @Override @ExceptionHandler(AccessDeniedException.class) public void handle(HttpServletRequest request, HttpServletResponse response,