All Downloads are FREE. Search and download functionalities are using the official Maven repository.

club.zhcs.lina.starter.exception.GlobalExceptionHandler Maven / Gradle / Ivy

There is a newer version: 3.3.4
Show newest version
package club.zhcs.lina.starter.exception;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ValidationException;

import org.nutz.lang.Strings;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.http.HttpStatus;
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.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import club.zhcs.lina.auth.AuthException;
import club.zhcs.lina.starter.exception.event.ExceptionEvent;

/**
 * 
 * @author Kerbores([email protected])
 *
 */
@RestController
@ControllerAdvice
public class GlobalExceptionHandler {

    @Autowired
    ApplicationEventPublisher applicationEventPublisher;

    Log log = Logs.get();

    @ExceptionHandler(value = Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public GlobalError defaultErrorHandler(HttpServletRequest request, Exception e) {
        log.error(e);
        applicationEventPublisher.publishEvent(ExceptionEvent.build(e));
        return GlobalError.builder()
                          .code(HttpStatus.INTERNAL_SERVER_ERROR.value())
                          .message("服务器发生错误,请稍后再试!")
                          .build();
    }

    @ExceptionHandler(value = BizException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public GlobalError bizExceptionHandler(HttpServletResponse response, Exception e) {
        log.error(e);
        return GlobalError.builder()
                          .code(HttpStatus.INTERNAL_SERVER_ERROR.value())
                          .message(e.getMessage())
                          .build();
    }

    @ExceptionHandler(ValidationException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public GlobalError handle(ValidationException e) {
        log.error(e);
        return GlobalError.builder()
                          .code(HttpStatus.BAD_REQUEST.value())
                          .message("请求参数校验不通过")
                          .build();
    }

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    @ResponseStatus(HttpStatus.BAD_REQUEST)
    public GlobalError validate(HttpServletResponse response, MethodArgumentNotValidException e) {
        log.error(e);
        return GlobalError.builder()
                          .code(HttpStatus.BAD_REQUEST.value())
                          .message("请求参数不正确")
                          .build();
    }

    @ExceptionHandler(value = AuthException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public GlobalError auth(HttpServletResponse response, AuthException e) {
        log.error(e);
        return GlobalError.builder()
                          .code(HttpStatus.FORBIDDEN.value())
                          .message(Strings.isBlank(e.getMessage()) ? "没有权限进行操作!" : e.getMessage())
                          .build();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy