From 80b6cbfc9c861469146318d0b3dd5f8b8b525b8a Mon Sep 17 00:00:00 2001 From: xiejun <xiejun@vci-tech.com> Date: 星期五, 01 十一月 2024 15:11:19 +0800 Subject: [PATCH] Revert "集成获取mdm分发通用数据格式接口集成" --- Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/RestExceptionTranslator.java | 147 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 147 insertions(+), 0 deletions(-) diff --git a/Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/RestExceptionTranslator.java b/Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/RestExceptionTranslator.java new file mode 100644 index 0000000..8dfede0 --- /dev/null +++ b/Source/BladeX-Tool/blade-starter-log/src/main/java/org/springblade/core/log/error/RestExceptionTranslator.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2018-2028, Chill Zhuang All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the dreamlu.net developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: Chill 搴勯獮 (smallchill@163.com) + */ +package org.springblade.core.log.error; + +import lombok.extern.slf4j.Slf4j; +import org.hibernate.validator.internal.engine.path.PathImpl; +import org.springblade.core.tool.api.R; +import org.springblade.core.tool.api.ResultCode; +import org.springblade.core.tool.utils.StringUtil; +import org.springframework.boot.autoconfigure.AutoConfiguration; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; +import org.springframework.http.HttpStatus; +import org.springframework.http.converter.HttpMessageNotReadableException; +import org.springframework.validation.BindException; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import org.springframework.web.HttpMediaTypeNotAcceptableException; +import org.springframework.web.HttpMediaTypeNotSupportedException; +import org.springframework.web.HttpRequestMethodNotSupportedException; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.MissingServletRequestParameterException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.ResponseStatus; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException; +import org.springframework.web.servlet.DispatcherServlet; +import org.springframework.web.servlet.NoHandlerFoundException; + +import javax.servlet.Servlet; +import javax.validation.ConstraintViolation; +import javax.validation.ConstraintViolationException; +import java.util.Set; + +/** + * 鍏ㄥ眬寮傚父澶勭悊锛屽鐞嗗彲棰勮鐨勫紓甯革紝Order 鎺掑簭浼樺厛绾ч珮 + * + * @author Chill + */ +@Slf4j +@Order(Ordered.HIGHEST_PRECEDENCE) +@AutoConfiguration +@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET) +@ConditionalOnClass({ Servlet.class, DispatcherServlet.class }) +@RestControllerAdvice +public class RestExceptionTranslator { + + @ExceptionHandler(MissingServletRequestParameterException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public R handleError(MissingServletRequestParameterException e) { + log.warn("缂哄皯璇锋眰鍙傛暟", e.getMessage()); + String message = String.format("缂哄皯蹇呰鐨勮姹傚弬鏁�: %s", e.getParameterName()); + return R.fail(ResultCode.PARAM_MISS, message); + } + + @ExceptionHandler(MethodArgumentTypeMismatchException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public R handleError(MethodArgumentTypeMismatchException e) { + log.warn("璇锋眰鍙傛暟鏍煎紡閿欒", e.getMessage()); + String message = String.format("璇锋眰鍙傛暟鏍煎紡閿欒: %s", e.getName()); + return R.fail(ResultCode.PARAM_TYPE_ERROR, message); + } + + @ExceptionHandler(MethodArgumentNotValidException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public R handleError(MethodArgumentNotValidException e) { + log.warn("鍙傛暟楠岃瘉澶辫触", e.getMessage()); + return handleError(e.getBindingResult()); + } + + @ExceptionHandler(BindException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public R handleError(BindException e) { + log.warn("鍙傛暟缁戝畾澶辫触", e.getMessage()); + return handleError(e.getBindingResult()); + } + + private R handleError(BindingResult result) { + FieldError error = result.getFieldError(); + String message = String.format("%s:%s", error.getField(), error.getDefaultMessage()); + return R.fail(ResultCode.PARAM_BIND_ERROR, message); + } + + @ExceptionHandler(ConstraintViolationException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public R handleError(ConstraintViolationException e) { + log.warn("鍙傛暟楠岃瘉澶辫触", e.getMessage()); + Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); + ConstraintViolation<?> violation = violations.iterator().next(); + String path = ((PathImpl) violation.getPropertyPath()).getLeafNode().getName(); + String message = String.format("%s:%s", path, violation.getMessage()); + return R.fail(ResultCode.PARAM_VALID_ERROR, message); + } + + @ExceptionHandler(NoHandlerFoundException.class) + @ResponseStatus(HttpStatus.NOT_FOUND) + public R handleError(NoHandlerFoundException e) { + log.error("404娌℃壘鍒拌姹�:{}", e.getMessage()); + return R.fail(ResultCode.NOT_FOUND, e.getMessage()); + } + + @ExceptionHandler(HttpMessageNotReadableException.class) + @ResponseStatus(HttpStatus.BAD_REQUEST) + public R handleError(HttpMessageNotReadableException e) { + log.error("娑堟伅涓嶈兘璇诲彇:{}", e.getMessage()); + return R.fail(ResultCode.MSG_NOT_READABLE, e.getMessage()); + } + + @ExceptionHandler(HttpRequestMethodNotSupportedException.class) + @ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED) + public R handleError(HttpRequestMethodNotSupportedException e) { + log.error("涓嶆敮鎸佸綋鍓嶈姹傛柟娉�:{}", e.getMessage()); + return R.fail(ResultCode.METHOD_NOT_SUPPORTED, e.getMessage()); + } + + @ExceptionHandler(HttpMediaTypeNotSupportedException.class) + @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) + public R handleError(HttpMediaTypeNotSupportedException e) { + log.error("涓嶆敮鎸佸綋鍓嶅獟浣撶被鍨�:{}", e.getMessage()); + return R.fail(ResultCode.MEDIA_TYPE_NOT_SUPPORTED, e.getMessage()); + } + + @ExceptionHandler(HttpMediaTypeNotAcceptableException.class) + @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) + public R handleError(HttpMediaTypeNotAcceptableException e) { + String message = e.getMessage() + " " + StringUtil.join(e.getSupportedMediaTypes()); + log.error("涓嶆帴鍙楃殑濯掍綋绫诲瀷:{}", message); + return R.fail(ResultCode.MEDIA_TYPE_NOT_SUPPORTED, message); + } + +} -- Gitblit v1.9.3