You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
bsdgy-server/src/main/java/com/kiisoo/ic/config/ExceptionAdvice.java

154 lines
5.1 KiB
Java

6 years ago
package com.kiisoo.ic.config;
import com.kiisoo.ic.common.BaseController;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageNotReadableException;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
*
*
* @author z
*/
@RestControllerAdvice
public class ExceptionAdvice extends BaseController {
/**
* Shiro
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(ShiroException.class)
public Map<String, Object> handle401(ShiroException e) {
return fail(HttpStatus.UNAUTHORIZED.value() + "", e.getMessage());
}
/**
* Shiro(UnauthorizedException)
* 访
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler({UnauthorizedException.class, AuthorizationException.class})
public Map<String, Object> handle401(UnauthorizedException e) {
return fail("40001", e.getMessage());
}
/**
* Shiro(UnauthenticatedException)
* 访
*
* @param e
* @return
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(UnauthenticatedException.class)
public Map<String, Object> handle401(UnauthenticatedException e) {
return fail("40001", "认证异常: " + e.getMessage());
}
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public Map<String, Object> validException(BindException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
Map<String, Object> result = this.getValidError(fieldErrors);
return fail("40006", result);
}
/**
* (MethodArgumentNotValidException)
*
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(MethodArgumentNotValidException.class)
public Map<String, Object> validException(MethodArgumentNotValidException e) {
List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
Map<String, Object> result = this.getValidError(fieldErrors);
return fail("406", result.get("errorList"));
}
/**
* 404
*
* @return
*/
@ResponseStatus(HttpStatus.NOT_FOUND)
@ExceptionHandler(NoHandlerFoundException.class)
public Map<String, Object> handle(NoHandlerFoundException e) {
return fail("404", "资源不存在");
}
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(HttpMessageNotReadableException.class)
public Map<String, Object> httpMessageNotReadableException(HttpServletRequest request, Throwable ex) {
return fail(this.getStatus(request).value() + "", "请求参数错误");
}
/**
*
*
* @param request
* @param ex
* @return
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public Map<String, Object> globalException(HttpServletRequest request, Throwable ex) {
ex.printStackTrace();
return fail(this.getStatus(request).value() + "", ex.toString() + ": " + ex.getMessage());
}
/**
*
*
* @param request
* @return
*/
private HttpStatus getStatus(HttpServletRequest request) {
Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
if (statusCode == null) {
return HttpStatus.INTERNAL_SERVER_ERROR;
}
return HttpStatus.valueOf(statusCode);
}
/**
*
*
* @param fieldErrors
* @return
*/
private Map<String, Object> getValidError(List<FieldError> fieldErrors) {
Map<String, Object> result = new HashMap<String, Object>(16);
List<String> errorList = new ArrayList<String>();
StringBuffer errorMsg = new StringBuffer();
for (FieldError error : fieldErrors) {
errorList.add(error.getDefaultMessage());
errorMsg.append(error.getDefaultMessage());
}
result.put("errorList", errorList);
result.put("errorMsg", errorMsg);
return result;
}
}