|
|
|
@ -15,7 +15,13 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.exception; |
|
|
|
|
|
|
|
import jakarta.servlet.RequestDispatcher; |
|
|
|
import jakarta.servlet.ServletException; |
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
import jakarta.servlet.http.HttpServletResponse; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.exception.ExceptionUtils; |
|
|
|
import org.springframework.boot.web.servlet.error.ErrorController; |
|
|
|
import org.springframework.http.HttpHeaders; |
|
|
|
import org.springframework.http.HttpStatus; |
|
|
|
import org.springframework.http.HttpStatusCode; |
|
|
|
@ -29,7 +35,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; |
|
|
|
@ -44,18 +52,18 @@ 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 jakarta.servlet.ServletException; |
|
|
|
import jakarta.servlet.http.HttpServletRequest; |
|
|
|
import jakarta.servlet.http.HttpServletResponse; |
|
|
|
import java.io.IOException; |
|
|
|
import java.util.HashMap; |
|
|
|
import java.util.Map; |
|
|
|
import java.util.Optional; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
@Controller |
|
|
|
@RestControllerAdvice |
|
|
|
public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler { |
|
|
|
public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHandler implements AccessDeniedHandler, ErrorController { |
|
|
|
|
|
|
|
private static final Map<HttpStatus, ThingsboardErrorCode> statusToErrorCodeMap = new HashMap<>(); |
|
|
|
|
|
|
|
static { |
|
|
|
statusToErrorCodeMap.put(HttpStatus.BAD_REQUEST, ThingsboardErrorCode.BAD_REQUEST_PARAMS); |
|
|
|
statusToErrorCodeMap.put(HttpStatus.UNAUTHORIZED, ThingsboardErrorCode.AUTHENTICATION); |
|
|
|
@ -68,7 +76,9 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand |
|
|
|
statusToErrorCodeMap.put(HttpStatus.INTERNAL_SERVER_ERROR, ThingsboardErrorCode.GENERAL); |
|
|
|
statusToErrorCodeMap.put(HttpStatus.SERVICE_UNAVAILABLE, ThingsboardErrorCode.GENERAL); |
|
|
|
} |
|
|
|
|
|
|
|
private static final Map<ThingsboardErrorCode, HttpStatus> errorCodeToStatusMap = new HashMap<>(); |
|
|
|
|
|
|
|
static { |
|
|
|
errorCodeToStatusMap.put(ThingsboardErrorCode.GENERAL, HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
errorCodeToStatusMap.put(ThingsboardErrorCode.AUTHENTICATION, HttpStatus.UNAUTHORIZED); |
|
|
|
@ -91,6 +101,17 @@ public class ThingsboardErrorResponseHandler extends ResponseEntityExceptionHand |
|
|
|
return errorCodeToStatusMap.getOrDefault(errorCode, HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
} |
|
|
|
|
|
|
|
@RequestMapping("/error") |
|
|
|
public ResponseEntity<Object> handleError(HttpServletRequest request) { |
|
|
|
HttpStatus httpStatus = Optional.ofNullable(request.getAttribute(RequestDispatcher.ERROR_STATUS_CODE)) |
|
|
|
.map(status -> HttpStatus.resolve(Integer.parseInt(status.toString()))) |
|
|
|
.orElse(HttpStatus.INTERNAL_SERVER_ERROR); |
|
|
|
String errorMessage = Optional.ofNullable(request.getAttribute(RequestDispatcher.ERROR_EXCEPTION)) |
|
|
|
.map(e -> (ExceptionUtils.getMessage((Throwable) e))) |
|
|
|
.orElse(httpStatus.getReasonPhrase()); |
|
|
|
return new ResponseEntity<>(ThingsboardErrorResponse.of(errorMessage, statusToErrorCode(httpStatus), httpStatus), httpStatus); |
|
|
|
} |
|
|
|
|
|
|
|
@Override |
|
|
|
@ExceptionHandler(AccessDeniedException.class) |
|
|
|
public void handle(HttpServletRequest request, HttpServletResponse response, |
|
|
|
|