| ¶Ô±ÈÐÂÎļþ | 
|  |  |  | 
|---|
|  |  |  | package com.doumee.config.handler; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import com.doumee.core.constants.ResponseStatus; | 
|---|
|  |  |  | import com.doumee.core.exception.BusinessException; | 
|---|
|  |  |  | import com.doumee.service.business.third.model.ApiResponse; | 
|---|
|  |  |  | import lombok.extern.slf4j.Slf4j; | 
|---|
|  |  |  | import org.apache.commons.lang3.StringUtils; | 
|---|
|  |  |  | import org.apache.shiro.authz.UnauthorizedException; | 
|---|
|  |  |  | import org.springframework.validation.BindingResult; | 
|---|
|  |  |  | import org.springframework.validation.FieldError; | 
|---|
|  |  |  | import org.springframework.web.bind.MethodArgumentNotValidException; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.ExceptionHandler; | 
|---|
|  |  |  | import org.springframework.web.bind.annotation.RestControllerAdvice; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import java.util.ArrayList; | 
|---|
|  |  |  | import java.util.List; | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * å
¨å±å¼å¸¸å¤ç | 
|---|
|  |  |  | * @author Eva.Caesar Liu | 
|---|
|  |  |  | * @date 2023/03/21 14:49 | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @Slf4j | 
|---|
|  |  |  | @RestControllerAdvice | 
|---|
|  |  |  | public class GlobalExceptionHandler { | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * ä¸å¡å¼å¸¸å¤ç | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @ExceptionHandler(BusinessException.class) | 
|---|
|  |  |  | public <T> ApiResponse<T> handleBusinessException (BusinessException e) { | 
|---|
|  |  |  | log.error(e.getMessage(), e); | 
|---|
|  |  |  | return ApiResponse.failed(e.getCode(), e.getMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * æ æéå¼å¸¸å¤ç | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @ExceptionHandler(UnauthorizedException.class) | 
|---|
|  |  |  | public <T> ApiResponse<T> handleUnauthorizedException (UnauthorizedException e) { | 
|---|
|  |  |  | log.error(e.getMessage(), e); | 
|---|
|  |  |  | return ApiResponse.failed("没ææä½æé"); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * åæ°éªè¯æªéè¿å¼å¸¸å¤ç | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @ExceptionHandler(MethodArgumentNotValidException.class) | 
|---|
|  |  |  | public <T> ApiResponse<T> handleMethodArgumentNotValidException (MethodArgumentNotValidException e) { | 
|---|
|  |  |  | log.error(e.getMessage(), e); | 
|---|
|  |  |  | BindingResult bindingResult = e.getBindingResult(); | 
|---|
|  |  |  | List<String> errors = new ArrayList<>(); | 
|---|
|  |  |  | for(FieldError fieldError : bindingResult.getFieldErrors()){ | 
|---|
|  |  |  | errors.add(fieldError.getDefaultMessage()); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | return ApiResponse.failed(ResponseStatus.BAD_REQUEST.getCode(), StringUtils.join(errors)); | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | /** | 
|---|
|  |  |  | * å
¶å®å¼å¸¸å¤ç | 
|---|
|  |  |  | */ | 
|---|
|  |  |  | @ExceptionHandler(Exception.class) | 
|---|
|  |  |  | public <T> ApiResponse<T> handleException (Exception e) { | 
|---|
|  |  |  | log.error(e.getMessage(), e); | 
|---|
|  |  |  | return ApiResponse.failed(ResponseStatus.SERVER_ERROR, e); | 
|---|
|  |  |  | } | 
|---|
|  |  |  | } | 
|---|