Skip to content

Commit

Permalink
restructure backend
Browse files Browse the repository at this point in the history
  • Loading branch information
HimitZH committed Mar 12, 2022
1 parent 504aef1 commit 7c60d54
Show file tree
Hide file tree
Showing 481 changed files with 15,488 additions and 9,254 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ docker ps # 查看当前运行的容器状态
| 2022-01-29 | 重构remote judge,增加AtCoder、SPOJ的支持 | Himit_ZH |
| 2022-02-19 | 修改首页前端布局和题目列表页 | Himit_ZH |
| 2022-02-25 | 支持PyPy2、PyPy3、JavaScript V8、JavaScript Node、PHP | Himit_ZH |
| 2022-03-12 | 后端接口全部重构,赛外榜单增加缓存 | Himit_ZH |

## 五、部分截图

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package top.hcode.hoj.common.exception;
package top.hcode.hoj.advice;


import com.google.protobuf.ServiceException;
Expand All @@ -22,7 +22,9 @@
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import top.hcode.hoj.common.exception.*;
import top.hcode.hoj.common.result.CommonResult;
import top.hcode.hoj.common.result.ResultStatus;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
Expand All @@ -41,18 +43,33 @@
*/
@Slf4j(topic = "hoj")
@RestControllerAdvice
public class GlobalExceptionHandler {
public class GlobalExceptionAdvice {


/**
* 400 - Internal Server Error 自定义通用异常
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = {StatusForbiddenException.class,
StatusAccessDeniedException.class,
StatusFailException.class,
StatusNotFoundException.class,
StatusSystemErrorException.class})
public CommonResult<Void> handleCustomException(Exception e) {
return CommonResult.errorResponse(e.getMessage(), ResultStatus.FAIL);
}


/**
* 401 -UnAuthorized 处理AuthenticationException,token相关异常 即是认证出错 可能无法处理!
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(value = AuthenticationException.class)
public CommonResult handleAuthenticationException(AuthenticationException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
public CommonResult<Void> handleAuthenticationException(AuthenticationException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
httpResponse.setHeader("Url-Type", httpRequest.getHeader("Url-Type")); // 为了前端能区别请求来源
return CommonResult.errorResponse(e.getMessage(), CommonResult.STATUS_ACCESS_DENIED);
return CommonResult.errorResponse(e.getMessage(), ResultStatus.ACCESS_DENIED);
}

/**
Expand All @@ -61,35 +78,35 @@ public CommonResult handleAuthenticationException(AuthenticationException e,
*/
@ResponseStatus(HttpStatus.UNAUTHORIZED)
@ExceptionHandler(value = UnauthenticatedException.class)
public CommonResult handleUnauthenticatedException(UnauthenticatedException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
public CommonResult<Void> handleUnauthenticatedException(UnauthenticatedException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
httpResponse.setHeader("Url-Type", httpRequest.getHeader("Url-Type")); // 为了前端能区别请求来源
return CommonResult.errorResponse("请您先登录!", CommonResult.STATUS_ACCESS_DENIED);
return CommonResult.errorResponse("请您先登录!", ResultStatus.ACCESS_DENIED);
}

/**
* 403 -FORBIDDEN AuthorizationException异常 即是授权认证出错 可能无法处理!
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(value = AuthorizationException.class)
public CommonResult handleAuthenticationException(AuthorizationException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
public CommonResult<Void> handleAuthenticationException(AuthorizationException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
httpResponse.setHeader("Url-Type", httpRequest.getHeader("Url-Type")); // 为了前端能区别请求来源
return CommonResult.errorResponse("对不起,您无权限进行此操作!", CommonResult.STATUS_FORBIDDEN);
return CommonResult.errorResponse("对不起,您无权限进行此操作!", ResultStatus.FORBIDDEN);
}

/**
* 403 -FORBIDDEN 处理shiro的异常 无法处理! 未能走到controller层
*/
@ResponseStatus(HttpStatus.FORBIDDEN)
@ExceptionHandler(value = ShiroException.class)
public CommonResult handleShiroException(ShiroException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
public CommonResult<Void> handleShiroException(ShiroException e,
HttpServletRequest httpRequest,
HttpServletResponse httpResponse) {
httpResponse.setHeader("Url-Type", httpRequest.getHeader("Url-Type")); // 为了前端能区别请求来源
return CommonResult.errorResponse("对不起,您无权限进行此操作,请先登录进行授权认证", CommonResult.STATUS_FORBIDDEN);
return CommonResult.errorResponse("对不起,您无权限进行此操作,请先登录进行授权认证", ResultStatus.FORBIDDEN);
}


Expand All @@ -98,19 +115,19 @@ public CommonResult handleShiroException(ShiroException e,
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = IllegalArgumentException.class)
public CommonResult handler(IllegalArgumentException e) {
return CommonResult.errorResponse(e.getMessage(), CommonResult.STATUS_FAIL);
public CommonResult<Void> handler(IllegalArgumentException e) {
return CommonResult.errorResponse(e.getMessage(), ResultStatus.FAIL);
}

/**
* 400 - Bad Request @Validated 校验错误异常处理
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public CommonResult handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) throws IOException {
public CommonResult<Void> handlerMethodArgumentNotValidException(MethodArgumentNotValidException e) throws IOException {
BindingResult bindingResult = e.getBindingResult();
ObjectError objectError = bindingResult.getAllErrors().stream().findFirst().get();
return CommonResult.errorResponse(objectError.getDefaultMessage(), CommonResult.STATUS_FAIL);
return CommonResult.errorResponse(objectError.getDefaultMessage(), ResultStatus.FAIL);
}


Expand All @@ -119,19 +136,19 @@ public CommonResult handlerMethodArgumentNotValidException(MethodArgumentNotVali
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
public CommonResult handleMissingServletRequestParameterException(
public CommonResult<Void> handleMissingServletRequestParameterException(
MissingServletRequestParameterException e) {
return CommonResult.errorResponse("The required request parameters are missing:" + e.getMessage(), CommonResult.STATUS_FAIL);
return CommonResult.errorResponse("The required request parameters are missing:" + e.getMessage(), ResultStatus.FAIL);
}

/**
* 400 - Bad Request 参数解析失败
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(HttpMessageNotReadableException.class)
public CommonResult handleHttpMessageNotReadableException(
public CommonResult<Void> handleHttpMessageNotReadableException(
HttpMessageNotReadableException e) {
return CommonResult.errorResponse("Failed to parse parameter format!", CommonResult.STATUS_FAIL);
return CommonResult.errorResponse("Failed to parse parameter format!", ResultStatus.FAIL);
}


Expand All @@ -140,53 +157,53 @@ public CommonResult handleHttpMessageNotReadableException(
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(BindException.class)
public CommonResult handleBindException(BindException e) {
public CommonResult<Void> handleBindException(BindException e) {
BindingResult result = e.getBindingResult();
FieldError error = result.getFieldError();
String field = error.getField();
String code = error.getDefaultMessage();
String message = String.format("%s:%s", field, code);
return CommonResult.errorResponse(message, CommonResult.STATUS_FAIL);
return CommonResult.errorResponse(message, ResultStatus.FAIL);
}

/**
* 400 - Bad Request 参数验证失败
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ConstraintViolationException.class)
public CommonResult handleServiceException(ConstraintViolationException e) {
public CommonResult<Void> handleServiceException(ConstraintViolationException e) {
Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
ConstraintViolation<?> violation = violations.iterator().next();
String message = violation.getMessage();
return CommonResult.errorResponse("[参数验证失败]parameter:" + message, CommonResult.STATUS_FAIL);
return CommonResult.errorResponse("[参数验证失败]parameter:" + message, ResultStatus.FAIL);
}

/**
* 400 - Bad Request 实体校验失败
*/
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(ValidationException.class)
public CommonResult handleValidationException(ValidationException e) {
return CommonResult.errorResponse("Entity verification failed. The request parameters are incorrect!", CommonResult.STATUS_FAIL);
public CommonResult<Void> handleValidationException(ValidationException e) {
return CommonResult.errorResponse("Entity verification failed. The request parameters are incorrect!", ResultStatus.FAIL);
}

/**
* 405 - Method Not Allowed 不支持当前请求方法
*/
@ResponseStatus(HttpStatus.METHOD_NOT_ALLOWED)
@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
public CommonResult handleHttpRequestMethodNotSupportedException(
public CommonResult<Void> handleHttpRequestMethodNotSupportedException(
HttpRequestMethodNotSupportedException e) {
return CommonResult.errorResponse("The request method is not supported!", 405);
return CommonResult.errorResponse("The request method is not supported!", ResultStatus.FAIL);
}

/**
* 415 - Unsupported Media Type 不支持当前媒体类型
*/
@ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE)
@ExceptionHandler(HttpMediaTypeNotSupportedException.class)
public CommonResult handleHttpMediaTypeNotSupportedException(Exception e) {
return CommonResult.errorResponse("The media type is not supported!", 415);
public CommonResult<Void> handleHttpMediaTypeNotSupportedException(Exception e) {
return CommonResult.errorResponse("The media type is not supported!", ResultStatus.FAIL);
}


Expand All @@ -195,29 +212,29 @@ public CommonResult handleHttpMediaTypeNotSupportedException(Exception e) {
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(value = MessagingException.class)
public CommonResult handler(MessagingException e){
public CommonResult<Void> handler(MessagingException e) {
log.error("邮箱系统异常-------------->{}", getMessage(e));
return CommonResult.errorResponse("Server Error! Please try Again later!", CommonResult.STATUS_ERROR);
return CommonResult.errorResponse("Server Error! Please try Again later!", ResultStatus.SYSTEM_ERROR);
}

/**
* 500 - Internal Server Error 业务逻辑异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(ServiceException.class)
public CommonResult handleServiceException(ServiceException e) {
public CommonResult<Void> handleServiceException(ServiceException e) {
log.error("业务逻辑异常-------------->{}", getMessage(e));
return CommonResult.errorResponse("Server Error! Please try Again later!", CommonResult.STATUS_ERROR);
return CommonResult.errorResponse("Server Error! Please try Again later!", ResultStatus.SYSTEM_ERROR);
}

/**
* 500 - Internal Server Error 操作数据库出现异常:名称重复,外键关联
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(DataIntegrityViolationException.class)
public CommonResult handleDataIntegrityViolationException(DataIntegrityViolationException e) {
public CommonResult<Void> handleDataIntegrityViolationException(DataIntegrityViolationException e) {
log.error("操作数据库出现异常-------------->{}", getMessage(e));
return CommonResult.errorResponse("Server Error! Please try Again later!", CommonResult.STATUS_ERROR);
return CommonResult.errorResponse("Server Error! Please try Again later!", ResultStatus.SYSTEM_ERROR);
}


Expand All @@ -226,29 +243,30 @@ public CommonResult handleDataIntegrityViolationException(DataIntegrityViolation
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(SQLException.class)
public CommonResult handleSQLException(SQLException e) {
public CommonResult<Void> handleSQLException(SQLException e) {
log.error("操作数据库出现异常-------------->{}", getMessage(e));
return CommonResult.errorResponse("Operation failed! Error message: " + e.getMessage(), CommonResult.STATUS_ERROR);
return CommonResult.errorResponse("Operation failed! Error message: " + e.getMessage(), ResultStatus.SYSTEM_ERROR);
}

/**
* 500 - Internal Server Error 批量操作数据库出现异常:名称重复,外键关联
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(PersistenceException.class)
public CommonResult handleBatchUpdateException(PersistenceException e) {
public CommonResult<Void> handleBatchUpdateException(PersistenceException e) {
log.error("操作数据库出现异常-------------->{}", getMessage(e));
return CommonResult.errorResponse("请检查数据是否准确!可能原因:数据库中已有相同的数据导致重复冲突!", CommonResult.STATUS_ERROR);
return CommonResult.errorResponse("请检查数据是否准确!可能原因:数据库中已有相同的数据导致重复冲突!", ResultStatus.SYSTEM_ERROR);
}


/**
* 500 - Internal Server Error 系统通用异常
*/
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
@ExceptionHandler(Exception.class)
public CommonResult handleException(Exception e) {
public CommonResult<Void> handleException(Exception e) {
log.error("系统通用异常-------------->{}", getMessage(e));
return CommonResult.errorResponse("Server Error! Please try Again later!", CommonResult.STATUS_ERROR);
return CommonResult.errorResponse("Server Error!", ResultStatus.SYSTEM_ERROR);
}


Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package top.hcode.hoj.common.exception;

/**
* @Author: Himit_ZH
* @Date: 2022/3/9 10:30
* @Description:
*/
public class StatusAccessDeniedException extends Exception {

public StatusAccessDeniedException() {
}

public StatusAccessDeniedException(String message) {
super(message);
}

public StatusAccessDeniedException(String message, Throwable cause) {
super(message, cause);
}

public StatusAccessDeniedException(Throwable cause) {
super(cause);
}

public StatusAccessDeniedException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package top.hcode.hoj.common.exception;

/**
* @Author: Himit_ZH
* @Date: 2022/3/9 10:27
* @Description:
*/
public class StatusFailException extends Exception{
public StatusFailException() {
}

public StatusFailException(String message) {
super(message);
}

public StatusFailException(String message, Throwable cause) {
super(message, cause);
}

public StatusFailException(Throwable cause) {
super(cause);
}

public StatusFailException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package top.hcode.hoj.common.exception;

/**
* @Author: Himit_ZH
* @Date: 2022/3/9 10:29
* @Description:
*/
public class StatusForbiddenException extends Exception{

public StatusForbiddenException() {
}

public StatusForbiddenException(String message) {
super(message);
}

public StatusForbiddenException(String message, Throwable cause) {
super(message, cause);
}

public StatusForbiddenException(Throwable cause) {
super(cause);
}

public StatusForbiddenException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Loading

0 comments on commit 7c60d54

Please sign in to comment.