io.github.firefang.power.exception.handler.ExceptionHandleUtil Maven / Gradle / Ivy
package io.github.firefang.power.exception.handler;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import io.github.firefang.power.exception.BadRequestException;
import io.github.firefang.power.exception.BusinessException;
import io.github.firefang.power.exception.NoPermissionException;
import io.github.firefang.power.exception.UnAuthorizedException;
import io.github.firefang.power.web.CommonResponse;
/**
* 异常处理工具类
*
* @author xinufo
*
*/
public class ExceptionHandleUtil {
private ExceptionHandleUtil() {
}
public static ResponseEntity> handle(UnAuthorizedException ex) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body(CommonResponse.fail(CommonResponse.UNAUTHORIZED, ex.getMessage()));
}
public static ResponseEntity handle(NoPermissionException ex) {
return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
}
public static ResponseEntity> handle(BusinessException ex, Logger log) {
String reason = "操作失败, " + ex.getMessage();
if (log.isWarnEnabled()) {
log.warn("业务异常: " + reason);
}
CommonResponse body = CommonResponse.fail(CommonResponse.BUSINESS_FAIL, reason);
return ResponseEntity.ok().body(body);
}
public static ResponseEntity> handle(BadRequestException ex) {
CommonResponse body = CommonResponse.fail(CommonResponse.BAD_REQUEST, ex.getMessage());
return ResponseEntity.badRequest().body(body);
}
public static ResponseEntity> handleUnknownException(Exception ex, Logger log) {
log.error("未知异常", ex);
CommonResponse body = CommonResponse.fail(CommonResponse.INTERNAL_ERROR, "未知异常");
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(body);
}
public static ResponseEntity