mirror of
https://github.com/201206030/novel-plus.git
synced 2025-07-06 01:06:39 +00:00
上传后台管理系统代码
This commit is contained in:
@ -0,0 +1,25 @@
|
||||
package com.java2nb.common.exception;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 自定义业务异常
|
||||
*/
|
||||
@Data
|
||||
public class BusinessException extends RuntimeException {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String msg;
|
||||
private int code;
|
||||
|
||||
public BusinessException(int code,String msg) {
|
||||
//不调用父类Throwable的fillInStackTrace()方法生成栈追踪信息,提高应用性能
|
||||
//构造器之间的调用必须在第一行
|
||||
super(msg, null, false, false);
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
@ -0,0 +1,81 @@
|
||||
package com.java2nb.common.exception;
|
||||
|
||||
import com.java2nb.common.config.Constant;
|
||||
import com.java2nb.common.domain.LogDO;
|
||||
import com.java2nb.common.service.LogService;
|
||||
import com.java2nb.common.utils.HttpServletUtils;
|
||||
import com.java2nb.common.utils.R;
|
||||
import com.java2nb.common.utils.ShiroUtils;
|
||||
import com.java2nb.system.domain.UserDO;
|
||||
import org.apache.shiro.authz.AuthorizationException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.dao.DuplicateKeyException;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* 异常处理器
|
||||
*/
|
||||
@RestControllerAdvice
|
||||
public class CommonExceptionHandler {
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
@Autowired
|
||||
LogService logService;
|
||||
|
||||
/**
|
||||
* 自定义业务异常处理
|
||||
*/
|
||||
@ExceptionHandler(BusinessException.class)
|
||||
public R handleBusinessException(BusinessException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
return R.error(e.getCode(),e.getMessage());
|
||||
}
|
||||
|
||||
@ExceptionHandler(DuplicateKeyException.class)
|
||||
public R handleDuplicateKeyException(DuplicateKeyException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
return R.error("数据库中已存在该记录");
|
||||
}
|
||||
|
||||
@ExceptionHandler(org.springframework.web.servlet.NoHandlerFoundException.class)
|
||||
public R noHandlerFoundException(org.springframework.web.servlet.NoHandlerFoundException e) {
|
||||
logger.error(e.getMessage(), e);
|
||||
return R.error(404, "没找找到页面");
|
||||
}
|
||||
|
||||
@ExceptionHandler(AuthorizationException.class)
|
||||
public Object handleAuthorizationException(AuthorizationException e, HttpServletRequest request) {
|
||||
logger.error(e.getMessage(), e);
|
||||
if (HttpServletUtils.jsAjax(request)) {
|
||||
return R.error(403, "未授权");
|
||||
}
|
||||
return new ModelAndView("error/403");
|
||||
}
|
||||
|
||||
|
||||
@ExceptionHandler({Exception.class})
|
||||
public Object handleException(Exception e, HttpServletRequest request) {
|
||||
LogDO logDO = new LogDO();
|
||||
logDO.setGmtCreate(new Date());
|
||||
logDO.setOperation(Constant.LOG_ERROR);
|
||||
logDO.setMethod(request.getRequestURL().toString());
|
||||
logDO.setParams(e.toString());
|
||||
UserDO current = ShiroUtils.getUser();
|
||||
if(null!=current){
|
||||
logDO.setUserId(current.getUserId());
|
||||
logDO.setUsername(current.getUsername());
|
||||
}
|
||||
logService.save(logDO);
|
||||
logger.error(e.getMessage(), e);
|
||||
if (HttpServletUtils.jsAjax(request)) {
|
||||
return R.error(500, "服务器错误,请联系管理员");
|
||||
}
|
||||
return new ModelAndView("error/500");
|
||||
}
|
||||
}
|
@ -0,0 +1,66 @@
|
||||
package com.java2nb.common.exception;
|
||||
|
||||
|
||||
import com.java2nb.common.utils.R;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.web.servlet.error.ErrorAttributes;
|
||||
import org.springframework.boot.web.servlet.error.ErrorController;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.ModelAndView;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import java.util.Map;
|
||||
|
||||
@RestController
|
||||
public class MainsiteErrorController implements ErrorController {
|
||||
private Logger logger = LoggerFactory.getLogger(getClass());
|
||||
private static final String ERROR_PATH = "/error";
|
||||
|
||||
@Autowired
|
||||
ErrorAttributes errorAttributes;
|
||||
|
||||
@RequestMapping(
|
||||
value = {ERROR_PATH},
|
||||
produces = {"text/html"}
|
||||
)
|
||||
public ModelAndView errorHtml(HttpServletRequest request, HttpServletResponse response) {
|
||||
int code = response.getStatus();
|
||||
if (404 == code) {
|
||||
return new ModelAndView("error/404");
|
||||
} else if (403 == code) {
|
||||
return new ModelAndView("error/403");
|
||||
} else if (401 == code) {
|
||||
return new ModelAndView("login");
|
||||
} else {
|
||||
return new ModelAndView("error/500");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@RequestMapping(value = ERROR_PATH)
|
||||
public R handleError(HttpServletRequest request, HttpServletResponse response) {
|
||||
response.setStatus(200);
|
||||
int code = response.getStatus();
|
||||
if (404 == code) {
|
||||
return R.error(404, "未找到资源");
|
||||
} else if (403 == code) {
|
||||
return R.error(403, "没有访问权限");
|
||||
} else if (401 == code) {
|
||||
return R.error(403, "登录过期");
|
||||
} else {
|
||||
return R.error(500, "服务器错误");
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getErrorPath() {
|
||||
// TODO Auto-generated method stub
|
||||
return ERROR_PATH;
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user