package com.cloud.retrieve.config; import lombok.extern.slf4j.Slf4j; import org.springframework.http.HttpStatus; import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ResponseStatus; import org.springframework.web.bind.annotation.RestControllerAdvice; import java.util.HashMap; import java.util.Map; @Slf4j @RestControllerAdvice public class ExceptionHandlerAdvice { @ExceptionHandler({ IllegalArgumentException.class }) @ResponseStatus(HttpStatus.BAD_REQUEST) public Map badRequestException(IllegalArgumentException exception) { exception.printStackTrace(); Map data = new HashMap<>(); data.put("code", HttpStatus.BAD_REQUEST.value()); data.put("msg", exception.getMessage()); // wp 修改 massage --> msg data.put("success",false); data.put("data",null); return data; } @ExceptionHandler({Exception.class, Throwable.class}) @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) public Map serverException(Throwable throwable) { throwable.printStackTrace(); Map data = new HashMap<>(); data.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value()); data.put("msg", "服务端异常,请联系管理员 ,错误信息:"+throwable.getLocalizedMessage()); data.put("success",false); // wp 修改 massage --> msg data.put("data",null); return data; } @ExceptionHandler({ SecurityException.class }) @ResponseStatus(HttpStatus.UNAUTHORIZED) public Map badRequestException(SecurityException exception) { Map data = new HashMap<>(); data.put("code", HttpStatus.UNAUTHORIZED.value()); data.put("msg", exception.getMessage()); // wp 修改 massage --> msg data.put("success",false); data.put("data",null); return data; } }