com.kasinf.framework.rest.web.controller.SearchableExceptionHandler Maven / Gradle / Ivy
package com.kasinf.framework.rest.web.controller;
import com.kasinf.framework.core.exception.ValidateException;
import com.kasinf.framework.core.response.ErrorResponse;
import com.kasinf.framework.core.response.HttpCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.validation.constraints.Null;
import java.util.Collections;
/**
* @author lkhsh
* 全局异常处理类
*/
@Slf4j
@ControllerAdvice
public class SearchableExceptionHandler {
@ExceptionHandler(ValidateException.class)
@ResponseBody
public ErrorResponse setErrorResponse(ValidateException validateException) {
log.error(validateException.getMessage(), validateException);
return new ErrorResponse(HttpCode.BAD_REQUEST, Collections.singleton(validateException.getErrors()));
}
@ExceptionHandler(RuntimeException.class)
@ResponseBody
public ErrorResponse setRuntimeErrorResponse(RuntimeException error) {
String message = error.getMessage();
if (error instanceof NullPointerException) {
message = "空指针异常";
}
log.error(message, error);
return new ErrorResponse(message);
}
}