You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@inlong.apache.org by he...@apache.org on 2022/06/05 12:38:20 UTC
[incubator-inlong] branch master updated: [INLONG-4505][Manager] Improve the return information of validation rules (#4506)
This is an automated email from the ASF dual-hosted git repository.
healchow pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-inlong.git
The following commit(s) were added to refs/heads/master by this push:
new 2f1378b1d [INLONG-4505][Manager] Improve the return information of validation rules (#4506)
2f1378b1d is described below
commit 2f1378b1d9d41e03cd8eba13356311534f0fadc6
Author: leosanqing <st...@qq.com>
AuthorDate: Sun Jun 5 20:38:15 2022 +0800
[INLONG-4505][Manager] Improve the return information of validation rules (#4506)
* Improve the return information of validation rules
* Extract a constant for controller exception info
Co-authored-by: healchow <he...@gmail.com>
---
.../web/config/ControllerExceptionHandler.java | 89 +++++++++++++---------
1 file changed, 51 insertions(+), 38 deletions(-)
diff --git a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/config/ControllerExceptionHandler.java b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/config/ControllerExceptionHandler.java
index 2276b4246..dfe7623ea 100644
--- a/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/config/ControllerExceptionHandler.java
+++ b/inlong-manager/manager-web/src/main/java/org/apache/inlong/manager/web/config/ControllerExceptionHandler.java
@@ -28,10 +28,10 @@ import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.validation.BindException;
+import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
-import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.ResponseBody;
+import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import javax.validation.ConstraintViolation;
@@ -39,105 +39,118 @@ import javax.validation.ConstraintViolationException;
import java.util.Set;
/**
- * Hander of controller exception.
+ * Handler of controller exception.
*/
@Slf4j
-@ControllerAdvice
+@RestControllerAdvice
public class ControllerExceptionHandler {
+ private static final String ERROR_MSG = "failed to handle request on path: %s by user: %s";
+
@ExceptionHandler(ConstraintViolationException.class)
- @ResponseBody
public Response<String> handleConstraintViolationException(HttpServletRequest request,
ConstraintViolationException e) {
+ UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
+
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
StringBuilder stringBuilder = new StringBuilder(64);
for (ConstraintViolation<?> violation : violations) {
stringBuilder.append(violation.getMessage()).append(".");
}
- UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
+
return Response.fail(stringBuilder.toString());
}
@ExceptionHandler(MethodArgumentNotValidException.class)
- @ResponseBody
public Response<String> handleMethodArgumentNotValidException(HttpServletRequest request,
MethodArgumentNotValidException e) {
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
- return Response.fail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
+
+ StringBuilder builder = new StringBuilder();
+ BindingResult result = e.getBindingResult();
+ result.getFieldErrors().forEach(
+ error -> builder.append(error.getField()).append(":")
+ .append(error.getDefaultMessage()).append(System.lineSeparator())
+ );
+
+ result.getGlobalErrors().forEach(
+ error -> builder.append(error.getDefaultMessage()).append(System.lineSeparator())
+ );
+
+ return Response.fail(builder.toString());
}
- @ResponseBody
@ExceptionHandler(value = IllegalArgumentException.class)
public Response<String> handleIllegalArgumentException(HttpServletRequest request, IllegalArgumentException e) {
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
return Response.fail(e.getMessage());
}
- @ResponseBody
@ExceptionHandler(value = BindException.class)
public Response<String> handleBindExceptionHandler(HttpServletRequest request, BindException e) {
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
- return Response.fail(e.getBindingResult().getAllErrors().get(0).getDefaultMessage());
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
+
+ StringBuilder builder = new StringBuilder();
+ e.getBindingResult().getFieldErrors().forEach(
+ error -> builder.append(error.getField()).append(":")
+ .append(error.getDefaultMessage()).append(System.lineSeparator())
+ );
+ return Response.fail(builder.toString());
}
- @ResponseBody
@ExceptionHandler(value = HttpMessageConversionException.class)
public Response<String> handleHttpMessageConversionExceptionHandler(HttpServletRequest request,
HttpMessageConversionException e) {
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
return Response.fail("http message convert exception! pls check params");
}
- @ResponseBody
@ExceptionHandler(value = WorkflowException.class)
public Response<String> handleWorkflowException(HttpServletRequest request, WorkflowException e) {
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
return Response.fail(e.getMessage());
}
- @ResponseBody
@ExceptionHandler(value = BusinessException.class)
public Response<String> handleBusinessExceptionHandler(HttpServletRequest request, BusinessException e) {
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
return Response.fail(e.getMessage());
}
- @ResponseBody
@ExceptionHandler(value = AuthenticationException.class)
public Response<String> handleAuthenticationException(HttpServletRequest request, AuthenticationException e) {
- log.error("Failed to handle request on path:" + request.getRequestURI(), e);
- return Response.fail("username or password is incorrect, or the account has expired");
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), ""), e);
+ return Response.fail("Username or password was incorrect, or the account has expired");
}
- @ResponseBody
@ExceptionHandler(value = UnauthorizedException.class)
public Response<String> handleUnauthorizedException(HttpServletRequest request, AuthorizationException e) {
- log.error("Failed to handle request on path:" + request.getRequestURI(), e);
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- return Response.fail("Current user [" + (userDetail != null ? userDetail.getUserName() : "")
- + "] has no permission to access URL: " + request.getRequestURI());
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
+ return Response.fail(String.format("Current user [%s] has no permission to access URL",
+ (userDetail != null ? userDetail.getUserName() : "")));
}
@ExceptionHandler(Exception.class)
- @ResponseBody
public Response<String> handle(HttpServletRequest request, Exception e) {
UserDetail userDetail = LoginUserUtils.getLoginUserDetail();
- log.error("Failed to handle request on path:" + request.getRequestURI()
- + (userDetail != null ? ", user:" + userDetail.getUserName() : ""), e);
+ String username = userDetail != null ? userDetail.getUserName() : "";
+ log.error(String.format(ERROR_MSG, request.getRequestURI(), username), e);
return Response.fail("There was an error in the service..."
+ "Please try again later! "
+ "If there are still problems, please contact the administrator");