异常处理优化
This commit is contained in:
parent
8087854336
commit
3593a17951
@ -1,34 +0,0 @@
|
||||
package cn.axzo.tyr.server.config.exception;
|
||||
|
||||
import cn.axzo.basics.common.exception.ServiceException;
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
import javax.validation.ConstraintViolation;
|
||||
import javax.validation.ConstraintViolationException;
|
||||
import java.util.Set;
|
||||
|
||||
@RestControllerAdvice
|
||||
@Slf4j
|
||||
public class ExceptionController {
|
||||
@ExceptionHandler(ServiceException.class)
|
||||
public ApiResult<Void> exceptionHandler(ServiceException e) {
|
||||
log.info("业务异常", e);
|
||||
return ApiResult.err(e.getErrorCode(), e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
@ResponseBody
|
||||
public ApiResult<String> handleConstraintViolationException(ConstraintViolationException exception) {
|
||||
Set<ConstraintViolation<?>> violations = exception.getConstraintViolations();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
for (ConstraintViolation violation : violations) {
|
||||
builder.append(violation.getMessage());
|
||||
break;
|
||||
}
|
||||
return ApiResult.err(builder.toString());
|
||||
}
|
||||
}
|
||||
@ -7,6 +7,8 @@ import cn.axzo.framework.autoconfigure.web.exception.handler.AbstractExceptionAp
|
||||
import cn.axzo.framework.domain.web.code.IRespCode;
|
||||
import cn.axzo.framework.domain.web.code.RespCode;
|
||||
import cn.axzo.framework.domain.web.result.ApiResult;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
@ -21,7 +23,7 @@ import java.util.Set;
|
||||
* @author wangli
|
||||
* @since 2023/9/11 11:39
|
||||
*/
|
||||
//@Component
|
||||
@Component
|
||||
public class ServiceExceptionResultHandler extends AbstractExceptionApiResultHandler<ServiceException> {
|
||||
public ServiceExceptionResultHandler(RespErrorCodeMappingProperties properties) {
|
||||
super(properties);
|
||||
@ -32,6 +34,11 @@ public class ServiceExceptionResultHandler extends AbstractExceptionApiResultHan
|
||||
return new RespCode(String.valueOf(error.getErrorCode()), error.getMessage());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected HttpStatus mappingHttpStatus(String code, ServiceException ex) {
|
||||
return HttpStatus.OK;
|
||||
}
|
||||
|
||||
@ExceptionHandler(ConstraintViolationException.class)
|
||||
@ResponseBody
|
||||
public ApiResult<String> handleConstraintViolationException(ConstraintViolationException exception) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user