From 5fce3d77c9fa1b56e22bbfaefb269e4ffb65b84d Mon Sep 17 00:00:00 2001 From: shi Date: Wed, 19 Feb 2025 16:41:44 +0800 Subject: [PATCH] =?UTF-8?q?refactor(exception):=20=E9=87=8D=E6=9E=84?= =?UTF-8?q?=E5=BC=82=E5=B8=B8=E5=A4=84=E7=90=86=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增基础异常类 ApiException - 定义错误码枚举 ErrorCode - 修改原有代码,使用 ApiException 替代 Assert 和 BaseException - 更新全局异常处理器,支持 ApiException 处理 --- .../ff/api/controller/ApiGameController.java | 43 +++++++------- .../api/controller/ApiMemberController.java | 15 ++--- .../service/impl/GamesJILIServiceImpl.java | 12 ++-- .../xk/service/impl/GamesXKServiceImpl.java | 13 +++-- .../java/com/ff/base/enums/ErrorCode.java | 48 +++++++++++++++ .../ff/base/exception/base/ApiException.java | 55 ++++++++++++++++++ .../web/exception/GlobalExceptionHandler.java | 45 +++++++------- .../exception/GlobalExceptionHandler.class | Bin 7044 -> 7434 bytes 8 files changed, 171 insertions(+), 60 deletions(-) create mode 100644 ff-base/src/main/java/com/ff/base/enums/ErrorCode.java create mode 100644 ff-base/src/main/java/com/ff/base/exception/base/ApiException.java diff --git a/ff-admin/src/main/java/com/ff/api/controller/ApiGameController.java b/ff-admin/src/main/java/com/ff/api/controller/ApiGameController.java index 1f05dbf..98f3909 100644 --- a/ff-admin/src/main/java/com/ff/api/controller/ApiGameController.java +++ b/ff-admin/src/main/java/com/ff/api/controller/ApiGameController.java @@ -11,8 +11,10 @@ import com.ff.base.constant.Constants; import com.ff.base.core.controller.BaseController; import com.ff.base.core.domain.AjaxResult; import com.ff.base.core.page.TableDataInfo; +import com.ff.base.enums.ErrorCode; import com.ff.base.enums.OperationType; import com.ff.base.enums.TransferType; +import com.ff.base.exception.base.ApiException; import com.ff.base.exception.base.BaseException; import com.ff.base.utils.StringUtils; import com.ff.base.utils.bean.BeanUtils; @@ -133,10 +135,10 @@ public class ApiGameController extends BaseController { public AjaxResult login(@Validated @RequestBody GameLoginRequest memberCreateApiRequest) { Game game = gameService.selectGameById(memberCreateApiRequest.getGameId()); - Assert.notNull(game, "游戏不存在"); + ApiException.notNull(game, ErrorCode.GAME_NOT_EXIST.getCode()); GamePlatform gamePlatform = gamePlatformService.selectGamePlatformById(game.getPlatformId()); - Assert.notNull(gamePlatform, "游戏平台不存在"); + ApiException.notNull(gamePlatform, ErrorCode.PLATFORM_NOT_EXIST.getCode()); IGamesService iGamesService = gamesService.get(gamePlatform.getPlatformCode() + Constants.SERVICE); @@ -144,13 +146,13 @@ public class ApiGameController extends BaseController { TenantSecretKey tenantSecretKey = keyConfig.get(); GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gamePlatform.getPlatformCode(), memberCreateApiRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); GameSecretKey gameSecretKeyLang = gameSecretKeyService.findByPlatformAndSystemLangCode(gamePlatform.getPlatformCode(), memberCreateApiRequest.getLangCode()); Assert.notNull(gameSecretKeyLang, "当前语言不存在"); Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(memberCreateApiRequest.getAccount(), memberCreateApiRequest.getCurrencyCode() + tenantSecretKey.getTenantSn())); - Assert.notNull(member, "会员不存在"); + ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode()); GamesLogin gamesLogin = GamesLogin.builder() .agentId(gameSecretKey.getCode()) @@ -185,11 +187,11 @@ public class ApiGameController extends BaseController { TenantSecretKey tenantSecretKey = keyConfig.get(); GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gameExchangeBalanceRequest.getPlatformCode(), gameExchangeBalanceRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(gameExchangeBalanceRequest.getAccount(), gameExchangeBalanceRequest.getCurrencyCode() + tenantSecretKey.getTenantSn())); - Assert.notNull(member, "会员不存在"); + ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode()); //操作租户额度 boolean isOut = gameExchangeBalanceRequest.getTransferType() == 1; @@ -260,16 +262,15 @@ public class ApiGameController extends BaseController { IGamesService iGamesService = gamesService.get(gameCreateFreeSpinRequest.getPlatformCode() + Constants.SERVICE); - Assert.notNull(iGamesService, "游戏平台不存在"); + ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode()); TenantSecretKey tenantSecretKey = keyConfig.get(); GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gameCreateFreeSpinRequest.getPlatformCode(), gameCreateFreeSpinRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); - + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(gameCreateFreeSpinRequest.getAccount(), gameCreateFreeSpinRequest.getCurrencyCode() + tenantSecretKey.getTenantSn())); - Assert.notNull(member, "会员不存在"); + ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode()); CreateFreeSpinRequestDTO createFreeSpinRequestDTO = CreateFreeSpinRequestDTO.builder() .account(member.getGameAccount()) .currency(gameCreateFreeSpinRequest.getCurrencyCode()) @@ -336,11 +337,11 @@ public class ApiGameController extends BaseController { GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gameGetDetailRequest.getPlatformCode(), gameGetDetailRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); IGamesService iGamesService = gamesService.get(gameGetDetailRequest.getPlatformCode() + Constants.SERVICE); - Assert.notNull(iGamesService, "游戏平台不存在"); + ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode()); GameSecretKey gameSecretKeyLang = gameSecretKeyService.findByPlatformAndSystemLangCode(gameGetDetailRequest.getPlatformCode(), gameGetDetailRequest.getLangCode()); Assert.notNull(gameSecretKeyLang, "当前语言不存在"); @@ -364,14 +365,14 @@ public class ApiGameController extends BaseController { @PostMapping("/kick/member") public AjaxResult kickMember(@Validated @RequestBody GameKickMemeberRequest gameKickMemeberRequest) { GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gameKickMemeberRequest.getPlatformCode(), gameKickMemeberRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); TenantSecretKey tenantSecretKey = keyConfig.get(); Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(gameKickMemeberRequest.getAccount(), gameKickMemeberRequest.getCurrencyCode() + tenantSecretKey.getTenantSn())); - Assert.notNull(member, "会员不存在"); + ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode()); IGamesService iGamesService = gamesService.get(gameKickMemeberRequest.getPlatformCode() + Constants.SERVICE); - Assert.notNull(iGamesService, "游戏平台不存在"); + ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode()); return AjaxResult.success(iGamesService.kickMember(KickMemberRequestDTO.builder() @@ -384,10 +385,10 @@ public class ApiGameController extends BaseController { @PostMapping("/kick/member/all") public AjaxResult kickMemberAll(@Validated @RequestBody GameKickMemeberAllRequest gameKickMemeberAllRequest) { GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gameKickMemeberAllRequest.getPlatformCode(), gameKickMemeberAllRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); IGamesService iGamesService = gamesService.get(gameKickMemeberAllRequest.getPlatformCode() + Constants.SERVICE); - Assert.notNull(iGamesService, "游戏平台不存在"); + ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode()); KickMemberAllDTO kickMemberAllDTO = KickMemberAllDTO.builder() .agentId(gameSecretKey.getCode()) @@ -395,7 +396,7 @@ public class ApiGameController extends BaseController { .build(); if (!ObjectUtils.isEmpty(gameKickMemeberAllRequest.getGameId())) { Game game = gameService.selectGameById(gameKickMemeberAllRequest.getGameId()); - Assert.notNull(game, "游戏不存在"); + ApiException.notNull(game, ErrorCode.GAME_NOT_EXIST.getCode()); kickMemberAllDTO.setGameId(game.getGameCode()); } @@ -446,10 +447,10 @@ public class ApiGameController extends BaseController { @PostMapping("/cancel/free/spin") public AjaxResult cancelFreeSpin(@Validated @RequestBody GameCancelFreeSpinRequest gameGetFreeSpinDashflowRequest) { GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(gameGetFreeSpinDashflowRequest.getPlatformCode(), gameGetFreeSpinDashflowRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); IGamesService iGamesService = gamesService.get(gameGetFreeSpinDashflowRequest.getPlatformCode() + Constants.SERVICE); - Assert.notNull(iGamesService, "游戏平台不存在"); + ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode()); Boolean cancelFreeSpin = iGamesService.cancelFreeSpin(CancelFreeSpinRequestDTO.builder() .agentId(gameSecretKey.getCode()) @@ -470,7 +471,7 @@ public class ApiGameController extends BaseController { TenantSecretKey tenantSecretKey = keyConfig.get(); Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(gameExchangeBalanceAllRequest.getAccount(), gameExchangeBalanceAllRequest.getCurrencyCode() + tenantSecretKey.getTenantSn())); - Assert.notNull(member, "会员不存在"); + ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode()); List gameSecretKeys = gameSecretKeyService.selectGameSecretKeyList(GameSecretKey.builder().systemCode(gameExchangeBalanceAllRequest.getCurrencyCode()).build()); diff --git a/ff-admin/src/main/java/com/ff/api/controller/ApiMemberController.java b/ff-admin/src/main/java/com/ff/api/controller/ApiMemberController.java index 5a6ca84..ed5bd78 100644 --- a/ff-admin/src/main/java/com/ff/api/controller/ApiMemberController.java +++ b/ff-admin/src/main/java/com/ff/api/controller/ApiMemberController.java @@ -11,6 +11,8 @@ import com.ff.api.response.MemberInfoResponse; import com.ff.base.constant.Constants; import com.ff.base.core.controller.BaseController; import com.ff.base.core.domain.AjaxResult; +import com.ff.base.enums.ErrorCode; +import com.ff.base.exception.base.ApiException; import com.ff.base.exception.base.BaseException; import com.ff.base.manager.AsyncManager; import com.ff.base.utils.StringUtils; @@ -90,12 +92,11 @@ public class ApiMemberController extends BaseController { public AjaxResult createMember(@Validated @RequestBody MemberCreateApiRequest memberCreateApiRequest) { IGamesService iGamesService = gamesService.get(memberCreateApiRequest.getPlatformCode() + Constants.SERVICE); - Assert.notNull(iGamesService, "平台不存在"); - + ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode()); TenantSecretKey tenantSecretKey = keyConfig.get(); GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(memberCreateApiRequest.getPlatformCode(), memberCreateApiRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); String gameAccount = StringUtils.addSuffix(memberCreateApiRequest.getAccount(), memberCreateApiRequest.getCurrencyCode() + tenantSecretKey.getTenantSn()); //向第三方注册账号 @@ -134,15 +135,15 @@ public class ApiMemberController extends BaseController { @PostMapping("/info") public AjaxResult getMemberInfo(@Validated @RequestBody MemberInfoApiRequest memberInfoApiRequest) { IGamesService iGamesService = gamesService.get(memberInfoApiRequest.getPlatformCode() + Constants.SERVICE); - Assert.notNull(iGamesService, "平台不存在"); + ApiException.notNull(iGamesService, ErrorCode.PLATFORM_NOT_EXIST.getCode()); TenantSecretKey tenantSecretKey = keyConfig.get(); GameSecretKey gameSecretKey = gameSecretKeyService.findSecretKeyByPlatformAndSystemCode(memberInfoApiRequest.getPlatformCode(), memberInfoApiRequest.getCurrencyCode()); - Assert.notNull(gameSecretKey, "货币游戏平台不存在"); + ApiException.notNull(gameSecretKey, ErrorCode.CURRENCY_NOT_EXIST.getCode()); Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(memberInfoApiRequest.getAccount(), memberInfoApiRequest.getCurrencyCode() + tenantSecretKey.getTenantSn())); - Assert.notNull(member, "会员不存在"); + ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode()); //向第三方查询账号 MemberInfoRequestDTO gamesBaseRequestDTO = MemberInfoRequestDTO.builder() @@ -168,7 +169,7 @@ public class ApiMemberController extends BaseController { TenantSecretKey tenantSecretKey = keyConfig.get(); Member member = memberService.selectMemberByGameAccount(StringUtils.addSuffix(memberInfoAllApiRequest.getAccount(), memberInfoAllApiRequest.getCurrencyCode() + tenantSecretKey.getTenantSn())); - Assert.notNull(member, "会员不存在"); + ApiException.notNull(member, ErrorCode.ACCOUNT_NOT_EXIST.getCode()); List gameSecretKeys = gameSecretKeyService.selectGameSecretKeyList(GameSecretKey.builder().systemCode(memberInfoAllApiRequest.getCurrencyCode()).build()); diff --git a/ff-admin/src/main/java/com/ff/game/api/jili/service/impl/GamesJILIServiceImpl.java b/ff-admin/src/main/java/com/ff/game/api/jili/service/impl/GamesJILIServiceImpl.java index 8a43d86..1bb1cbb 100644 --- a/ff-admin/src/main/java/com/ff/game/api/jili/service/impl/GamesJILIServiceImpl.java +++ b/ff-admin/src/main/java/com/ff/game/api/jili/service/impl/GamesJILIServiceImpl.java @@ -6,6 +6,7 @@ import com.ff.base.constant.CacheConstants; import com.ff.base.constant.Constants; import com.ff.base.core.redis.RedisCache; import com.ff.base.enums.*; +import com.ff.base.exception.base.ApiException; import com.ff.base.exception.base.BaseException; import com.ff.base.system.service.ISysConfigService; import com.ff.base.utils.DateUtils; @@ -140,12 +141,15 @@ public class GamesJILIServiceImpl implements IGamesService { createMemberRequestDTO.setQuery(query); String key = this.getKey(createMemberRequestDTO); JILICreateMemberResponseDTO createMemberResponseDTO = jiliClient.createMember(query + "&Key=" + key); - Boolean isSuccess = this.getIsSuccess(createMemberResponseDTO.getErrorCode()); - if (!isSuccess) { - throw new BaseException(createMemberResponseDTO.getMessage()); + int errorCode = createMemberResponseDTO.getErrorCode(); + if (0 == errorCode){ + return Boolean.TRUE; + } + if (101 == errorCode){ + throw new ApiException(ErrorCode.GAME_ACCOUNT_CREATION_FAILED.getCode()); } //判断是否获取成功 - return isSuccess; + return Boolean.FALSE; } diff --git a/ff-admin/src/main/java/com/ff/game/api/xk/service/impl/GamesXKServiceImpl.java b/ff-admin/src/main/java/com/ff/game/api/xk/service/impl/GamesXKServiceImpl.java index 0bd5c30..d374529 100644 --- a/ff-admin/src/main/java/com/ff/game/api/xk/service/impl/GamesXKServiceImpl.java +++ b/ff-admin/src/main/java/com/ff/game/api/xk/service/impl/GamesXKServiceImpl.java @@ -5,9 +5,11 @@ import com.alibaba.fastjson2.JSON; import com.ff.base.constant.CacheConstants; import com.ff.base.constant.Constants; import com.ff.base.core.redis.RedisCache; +import com.ff.base.enums.ErrorCode; import com.ff.base.enums.GamePlatforms; import com.ff.base.enums.GameStatus; import com.ff.base.enums.XKGameType; +import com.ff.base.exception.base.ApiException; import com.ff.base.exception.base.BaseException; import com.ff.base.system.service.ISysConfigService; import com.ff.base.utils.DateUtils; @@ -142,12 +144,15 @@ public class GamesXKServiceImpl implements IGamesService { String key = this.getKey(createMemberRequestDTO); params.put("key", key); XKCreateMemberResponseDTO xkCreateMemberResponseDTO = xkClient.createMember(params); - Boolean isSuccess = this.getIsSuccess(xkCreateMemberResponseDTO.getCode()); - if (!isSuccess) { - throw new BaseException(xkCreateMemberResponseDTO.getMsg()); + int errorCode = xkCreateMemberResponseDTO.getCode(); + if (0 == errorCode){ + return Boolean.TRUE; + } + if (101 == errorCode){ + throw new ApiException(ErrorCode.GAME_ACCOUNT_CREATION_FAILED.getCode()); } //判断是否获取成功 - return isSuccess; + return Boolean.FALSE; } diff --git a/ff-base/src/main/java/com/ff/base/enums/ErrorCode.java b/ff-base/src/main/java/com/ff/base/enums/ErrorCode.java new file mode 100644 index 0000000..aa13450 --- /dev/null +++ b/ff-base/src/main/java/com/ff/base/enums/ErrorCode.java @@ -0,0 +1,48 @@ +package com.ff.base.enums; + +import lombok.Getter; + +import java.util.Optional; +import java.util.stream.Stream; + +/** + * 错误代码 + * + * @author shi + * @date 2025/02/19 + */ +@Getter +public enum ErrorCode { + ERROR(500, "系统业务异常"), + GAME_ACCOUNT_CREATION_FAILED(1001, "当前游戏账号已存在"), + ACCOUNT_NOT_EXIST(1002, "当前游戏账号不存在"), + PLATFORM_NOT_EXIST(1003, "游戏平台不存在"), + CURRENCY_NOT_EXIST(1004, "游戏平台不支持的货币"), + GAME_NOT_EXIST(1005, "游戏不存在"), + ; + + // 获取错误码 + private final int code; + // 获取错误信息 + private final String message; + + // 构造函数 + ErrorCode(int code, String message) { + this.code = code; + this.message = message; + } + + /** + * 按代码查找系统 + * + * @param code 代码 + * @return {@link String } + */ + public static String findByCode(Integer code) { + Optional system = Stream.of(ErrorCode.values()) + .filter(errorCode -> errorCode.getCode() == code) + .map(ErrorCode::getMessage) + .findFirst(); + return system.orElse(null); + } +} \ No newline at end of file diff --git a/ff-base/src/main/java/com/ff/base/exception/base/ApiException.java b/ff-base/src/main/java/com/ff/base/exception/base/ApiException.java new file mode 100644 index 0000000..4b0b63b --- /dev/null +++ b/ff-base/src/main/java/com/ff/base/exception/base/ApiException.java @@ -0,0 +1,55 @@ +package com.ff.base.exception.base; + +import com.ff.base.enums.ErrorCode; +import com.ff.base.utils.MessageUtils; +import com.ff.base.utils.StringUtils; +import lombok.Getter; +import org.springframework.lang.Nullable; + +/** + * 基础异常 + * + * @author ff + */ +@Getter +public class ApiException extends RuntimeException { + private static final long serialVersionUID = 1L; + + + /** + * 错误码 + */ + private Integer code; + + + /** + * 错误消息 + */ + private String message; + + public ApiException(Integer code, String message) { + this.code = code; + this.message = message; + } + public ApiException(Integer code) { + this.code = code; + this.message = ErrorCode.findByCode(code); + } + + @Override + public String getMessage() { + return message; + } + + /** + * 非空 + * + * @param object 对象 + * @param code 代码 + */ + public static void notNull(@Nullable Object object, Integer code) { + if (object == null) { + throw new ApiException(code); + } + } +} diff --git a/ff-base/src/main/java/com/ff/base/web/exception/GlobalExceptionHandler.java b/ff-base/src/main/java/com/ff/base/web/exception/GlobalExceptionHandler.java index a8500de..298e992 100644 --- a/ff-base/src/main/java/com/ff/base/web/exception/GlobalExceptionHandler.java +++ b/ff-base/src/main/java/com/ff/base/web/exception/GlobalExceptionHandler.java @@ -5,6 +5,8 @@ import com.ff.base.core.domain.AjaxResult; import com.ff.base.core.text.Convert; import com.ff.base.exception.DemoModeException; import com.ff.base.exception.ServiceException; +import com.ff.base.exception.base.ApiException; +import com.ff.base.exception.base.BaseException; import com.ff.base.utils.StringUtils; import com.ff.base.utils.html.EscapeUtil; import org.slf4j.Logger; @@ -26,16 +28,14 @@ import javax.servlet.http.HttpServletRequest; * @author ff */ @RestControllerAdvice -public class GlobalExceptionHandler -{ +public class GlobalExceptionHandler { private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class); /** * 权限校验异常 */ @ExceptionHandler(AccessDeniedException.class) - public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) - { + public AjaxResult handleAccessDeniedException(AccessDeniedException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',权限校验失败'{}'", requestURI, e.getMessage()); return AjaxResult.error(HttpStatus.FORBIDDEN, "没有权限,请联系管理员授权"); @@ -46,8 +46,7 @@ public class GlobalExceptionHandler */ @ExceptionHandler(HttpRequestMethodNotSupportedException.class) public AjaxResult handleHttpRequestMethodNotSupported(HttpRequestMethodNotSupportedException e, - HttpServletRequest request) - { + HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',不支持'{}'请求", requestURI, e.getMethod()); return AjaxResult.error(e.getMessage()); @@ -57,8 +56,7 @@ public class GlobalExceptionHandler * 业务异常 */ @ExceptionHandler(ServiceException.class) - public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) - { + public AjaxResult handleServiceException(ServiceException e, HttpServletRequest request) { log.error(e.getMessage(), e); Integer code = e.getCode(); return StringUtils.isNotNull(code) ? AjaxResult.error(code, e.getMessage()) : AjaxResult.error(e.getMessage()); @@ -68,8 +66,7 @@ public class GlobalExceptionHandler * 请求路径中缺少必需的路径变量 */ @ExceptionHandler(MissingPathVariableException.class) - public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) - { + public AjaxResult handleMissingPathVariableException(MissingPathVariableException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求路径中缺少必需的路径变量'{}',发生系统异常.", requestURI, e); return AjaxResult.error(String.format("请求路径中缺少必需的路径变量[%s]", e.getVariableName())); @@ -79,12 +76,10 @@ public class GlobalExceptionHandler * 请求参数类型不匹配 */ @ExceptionHandler(MethodArgumentTypeMismatchException.class) - public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) - { + public AjaxResult handleMethodArgumentTypeMismatchException(MethodArgumentTypeMismatchException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); String value = Convert.toStr(e.getValue()); - if (StringUtils.isNotEmpty(value)) - { + if (StringUtils.isNotEmpty(value)) { value = EscapeUtil.clean(value); } log.error("请求参数类型不匹配'{}',发生系统异常.", requestURI, e); @@ -95,19 +90,24 @@ public class GlobalExceptionHandler * 拦截未知的运行时异常 */ @ExceptionHandler(RuntimeException.class) - public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) - { + public AjaxResult handleRuntimeException(RuntimeException e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生未知异常.", requestURI, e); return AjaxResult.error(e.getMessage()); } + @ExceptionHandler(ApiException.class) + public AjaxResult handleBaseException(ApiException e, HttpServletRequest request) { + String requestURI = request.getRequestURI(); + log.error("请求地址'{}',发生未知异常.", requestURI, e); + return AjaxResult.error(e.getCode(), e.getMessage()); + } + /** * 系统异常 */ @ExceptionHandler(Exception.class) - public AjaxResult handleException(Exception e, HttpServletRequest request) - { + public AjaxResult handleException(Exception e, HttpServletRequest request) { String requestURI = request.getRequestURI(); log.error("请求地址'{}',发生系统异常.", requestURI, e); return AjaxResult.error(e.getMessage()); @@ -117,8 +117,7 @@ public class GlobalExceptionHandler * 自定义验证异常 */ @ExceptionHandler(BindException.class) - public AjaxResult handleBindException(BindException e) - { + public AjaxResult handleBindException(BindException e) { log.error(e.getMessage(), e); String message = e.getAllErrors().get(0).getDefaultMessage(); return AjaxResult.error(message); @@ -128,8 +127,7 @@ public class GlobalExceptionHandler * 自定义验证异常 */ @ExceptionHandler(MethodArgumentNotValidException.class) - public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) - { + public Object handleMethodArgumentNotValidException(MethodArgumentNotValidException e) { log.error(e.getMessage(), e); String message = e.getBindingResult().getFieldError().getDefaultMessage(); return AjaxResult.error(message); @@ -139,8 +137,7 @@ public class GlobalExceptionHandler * 演示模式异常 */ @ExceptionHandler(DemoModeException.class) - public AjaxResult handleDemoModeException(DemoModeException e) - { + public AjaxResult handleDemoModeException(DemoModeException e) { return AjaxResult.error("演示模式,不允许操作"); } } diff --git a/ff-base/target/classes/com/ff/base/web/exception/GlobalExceptionHandler.class b/ff-base/target/classes/com/ff/base/web/exception/GlobalExceptionHandler.class index f368c9f3d8ba19a657d6bbde41f57acc1e5b5ee4..586e82c2f1babd4b1902900f85713a5c026e6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