package com.cloud.application.config;
|
|
import lombok.extern.log4j.Log4j;
|
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;
|
|
@Log4j
|
@RestControllerAdvice
|
public class ExceptionHandlerAdvice {
|
|
@ExceptionHandler({ IllegalArgumentException.class })
|
@ResponseStatus(HttpStatus.BAD_REQUEST)
|
public Map<String, Object> badRequestException(IllegalArgumentException exception) {
|
Map<String, Object> data = new HashMap<>();
|
data.put("code", HttpStatus.BAD_REQUEST.value());
|
data.put("message", exception.getMessage());
|
|
return data;
|
}
|
|
|
@ExceptionHandler({Exception.class, Throwable.class})
|
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
public Map<String, Object> serverException(Throwable throwable) {
|
Map<String, Object> data = new HashMap<>();
|
data.put("code", HttpStatus.INTERNAL_SERVER_ERROR.value());
|
data.put("message", "服务端异常,请联系管理员 ,错误信息:"+throwable.getLocalizedMessage());
|
|
return data;
|
}
|
|
@ExceptionHandler({ SecurityException.class })
|
@ResponseStatus(HttpStatus.UNAUTHORIZED)
|
public Map<String, Object> badRequestException(SecurityException exception) {
|
Map<String, Object> data = new HashMap<>();
|
data.put("code", HttpStatus.UNAUTHORIZED.value());
|
data.put("message", exception.getMessage());
|
|
return data;
|
}
|
}
|