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

top.lshaci.framework.web.handler.exception.GlobalExceptionHandler Maven / Gradle / Ivy

package top.lshaci.framework.web.handler.exception;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
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.RestController;
import top.lshaci.framework.common.exception.BaseException;
import top.lshaci.framework.web.enums.ErrorCode;
import top.lshaci.framework.web.model.JsonResponse;

import javax.servlet.http.HttpServletRequest;

/**
 * Global exception handler

* * 0.0.4: Add method argumentExceptionHandler * * @author lshaci * @since 0.0.3 * @version 0.0.4 */ @Slf4j @RestController @ControllerAdvice public class GlobalExceptionHandler { /** * System exception log message */ private static final String SYSTEM_EXCEPTION = "System anomaly!"; /** * Argument exception log message */ private final static String FIELD = "字段:"; /** * Base exception handler * * @param req the http servlet request * @param e the exception * @return json response */ @ExceptionHandler(BaseException.class) public JsonResponse baseExceptionHandler(HttpServletRequest req, Exception e) { log.error(SYSTEM_EXCEPTION, e); return JsonResponse .failure(e.getMessage()) .setCode(ErrorCode.INTERNAL_PROGRAM_ERROR.getCode()); } /** * BindException and MethodArgumentNotValidException handler * * @param req the http servlet request * @param e the exception * @return json response */ @ExceptionHandler(value = { BindException.class, MethodArgumentNotValidException.class }) public JsonResponse argumentExceptionHandler(HttpServletRequest req, Exception e) { log.error(SYSTEM_EXCEPTION, e); StringBuilder message = new StringBuilder(); FieldError fieldError = null; if (e instanceof BindException) { BindException bindException = (BindException) e; fieldError = bindException.getBindingResult().getFieldErrors().get(0); } else if (e instanceof MethodArgumentNotValidException) { MethodArgumentNotValidException argumentNotValidException = (MethodArgumentNotValidException) e; fieldError = argumentNotValidException.getBindingResult().getFieldErrors().get(0); } if (fieldError != null) { String field = fieldError.getField(); log.warn(FIELD + field); String msg = fieldError.getDefaultMessage(); message.append(msg); } return JsonResponse.failure(message.toString()); } /** * Default exception handler * * @param req the http servlet request * @param e the exception * @return json response */ @ExceptionHandler(Exception.class) public JsonResponse defaultExceptionHandler(HttpServletRequest req, Exception e) { log.error(SYSTEM_EXCEPTION, e); ErrorCode errorCode = ErrorCode.getByException(e); return JsonResponse .failure(errorCode.getMsg()) .setCode(errorCode.getCode()) .addOtherData("detail", e.getMessage()); } }