我已經(jīng)不用 try catch 處理異常了!太辣雞了!
# 背景
# 什么是統(tǒng)一異常處理
# 目標
# 統(tǒng)一異常處理實戰(zhàn)
用 Assert(斷言) 替換 throw exception
@Testpublic void test1() {...User user = userDao.selectById(userId);Assert.notNull(user, "用戶不存在.");...}public void test2() {// 另一種寫法User user = userDao.selectById(userId);if (user == null) {throw new IllegalArgumentException("用戶不存在.");}}
public abstract class Assert {public Assert() {}public static void notNull(@Nullable Object object, String message) {if (object == null) {throw new IllegalArgumentException(message);}}}
Assertpublic interface Assert {/*** 創(chuàng)建異常* @param args* @return*/BaseException newException(Object... args);/*** 創(chuàng)建異常* @param t* @param args* @return*/BaseException newException(Throwable t, Object... args);/***斷言對象
obj非空。如果對象obj為空,則拋出異常** @param obj 待判斷對象*/default void assertNotNull(Object obj) {if (obj == null) {throw newException(obj);}}/***斷言對象
obj非空。如果對象obj為空,則拋出異常*異常信息
message支持傳遞參數(shù)方式,避免在判斷之前進行字符串拼接操作** @param obj 待判斷對象* @param args message占位符對應的參數(shù)列表*/default void assertNotNull(Object obj, Object... args) {if (obj == null) {throw newException(args);}}}
善解人意的Enum
public interface IResponseEnum {int getCode();String getMessage();}/***業(yè)務異常
*業(yè)務處理時,出現(xiàn)異常,可以拋出該異常
*/public class BusinessException extends BaseException {private static final long serialVersionUID = 1L;public BusinessException(IResponseEnum responseEnum, Object[] args, String message) {super(responseEnum, args, message);}public BusinessException(IResponseEnum responseEnum, Object[] args, String message, Throwable cause) {super(responseEnum, args, message, cause);}}public interface BusinessExceptionAssert extends IResponseEnum, Assert {default BaseException newException(Object... args) {String msg = MessageFormat.format(this.getMessage(), args);return new BusinessException(this, args, msg);}default BaseException newException(Throwable t, Object... args) {String msg = MessageFormat.format(this.getMessage(), args);return new BusinessException(this, args, msg, t);}}@Getter@AllArgsConstructorpublic enum ResponseEnum implements BusinessExceptionAssert {/*** Bad licence type*/BAD_LICENCE_TYPE(7001, "Bad licence type."),/*** Licence not found*/LICENCE_NOT_FOUND(7002, "Licence not found.");/*** 返回碼*/private int code;/*** 返回消息*/private String message;}
/*** 校驗{@link Licence}存在* @param licence*/private void checkNotNull(Licence licence) {ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(licence);}
private void checkNotNull(Licence licence) {if (licence == null) {throw new LicenceNotFoundException();// 或者這樣throw new BusinessException(7001, "Bad licence type.");}}
注:上面舉的例子是針對特定的業(yè)務,而有部分異常情況是通用的,比如:服務器繁忙、網(wǎng)絡異常、服務器異常、參數(shù)校驗異常、404等,所以有CommonResponseEnum、ArgumentResponseEnum、ServletResponseEnum,其中 ServletResponseEnum 會在后文詳細說明。
定義統(tǒng)一異常處理器類
4j@Component@ControllerAdvice@ConditionalOnWebApplication(UnifiedExceptionHandler.class)public class UnifiedExceptionHandler {/*** 生產(chǎn)環(huán)境*/private final static String ENV_PROD = "prod";private UnifiedMessageSource unifiedMessageSource;/*** 當前環(huán)境*/("${spring.profiles.active}")private String profile;/*** 獲取國際化消息** @param e 異常* @return*/public String getMessage(BaseException e) {String code = "response." + e.getResponseEnum().toString();String message = unifiedMessageSource.getMessage(code, e.getArgs());if (message == null || message.isEmpty()) {return e.getMessage();}return message;}/*** 業(yè)務異常** @param e 異常* @return 異常結果*/(value = BusinessException.class)public ErrorResponse handleBusinessException(BaseException e) {log.error(e.getMessage(), e);return new ErrorResponse(e.getResponseEnum().getCode(), getMessage(e));}/*** 自定義異常** @param e 異常* @return 異常結果*/(value = BaseException.class)public ErrorResponse handleBaseException(BaseException e) {log.error(e.getMessage(), e);return new ErrorResponse(e.getResponseEnum().getCode(), getMessage(e));}/*** Controller上一層相關異常** @param e 異常* @return 異常結果*/({NoHandlerFoundException.class,HttpRequestMethodNotSupportedException.class,HttpMediaTypeNotSupportedException.class,MissingPathVariableException.class,MissingServletRequestParameterException.class,TypeMismatchException.class,HttpMessageNotReadableException.class,HttpMessageNotWritableException.class,// BindException.class,// MethodArgumentNotValidException.classHttpMediaTypeNotAcceptableException.class,ServletRequestBindingException.class,ConversionNotSupportedException.class,MissingServletRequestPartException.class,AsyncRequestTimeoutException.class})public ErrorResponse handleServletException(Exception e) {log.error(e.getMessage(), e);int code = CommonResponseEnum.SERVER_ERROR.getCode();try {ServletResponseEnum servletExceptionEnum = ServletResponseEnum.valueOf(e.getClass().getSimpleName());code = servletExceptionEnum.getCode();} catch (IllegalArgumentException e1) {log.error("class [{}] not defined in enum {}", e.getClass().getName(), ServletResponseEnum.class.getName());}if (ENV_PROD.equals(profile)) {// 當為生產(chǎn)環(huán)境, 不適合把具體的異常信息展示給用戶, 比如404.code = CommonResponseEnum.SERVER_ERROR.getCode();BaseException baseException = new BaseException(CommonResponseEnum.SERVER_ERROR);String message = getMessage(baseException);return new ErrorResponse(code, message);}return new ErrorResponse(code, e.getMessage());}/*** 參數(shù)綁定異常** @param e 異常* @return 異常結果*/(value = BindException.class)public ErrorResponse handleBindException(BindException e) {log.error("參數(shù)綁定校驗異常", e);return wrapperBindingResult(e.getBindingResult());}/*** 參數(shù)校驗異常,將校驗失敗的所有異常組合成一條錯誤信息** @param e 異常* @return 異常結果*/(value = MethodArgumentNotValidException.class)public ErrorResponse handleValidException(MethodArgumentNotValidException e) {log.error("參數(shù)綁定校驗異常", e);return wrapperBindingResult(e.getBindingResult());}/*** 包裝綁定異常結果** @param bindingResult 綁定結果* @return 異常結果*/private ErrorResponse wrapperBindingResult(BindingResult bindingResult) {StringBuilder msg = new StringBuilder();for (ObjectError error : bindingResult.getAllErrors()) {msg.append(", ");if (error instanceof FieldError) {msg.append(((FieldError) error).getField()).append(": ");}msg.append(error.getDefaultMessage() == null ? "" : error.getDefaultMessage());}return new ErrorResponse(ArgumentResponseEnum.VALID_ERROR.getCode(), msg.substring(2));}/*** 未定義異常** @param e 異常* @return 異常結果*/(value = Exception.class)public ErrorResponse handleException(Exception e) {log.error(e.getMessage(), e);if (ENV_PROD.equals(profile)) {// 當為生產(chǎn)環(huán)境, 不適合把具體的異常信息展示給用戶, 比如數(shù)據(jù)庫異常信息.int code = CommonResponseEnum.SERVER_ERROR.getCode();BaseException baseException = new BaseException(CommonResponseEnum.SERVER_ERROR);String message = getMessage(baseException);return new ErrorResponse(code, message);????????}return new ErrorResponse(CommonResponseEnum.SERVER_ERROR.getCode(), e.getMessage());????}}
進入Controller前的異常: handleServletException、handleBindException、handleValidException 自定義異常: handleBusinessException、handleBaseException 未知異常: handleException
# 異常處理器說明
handleServletException
handleBindException
handleValidException
handleBusinessException、handleBaseException
handleException
注:上面的handleServletException、handleException 這兩個處理器,返回的異常信息,不同環(huán)境返回的可能不一樣,以為這些異常信息都是框架自帶的異常信息,一般都是英文的,不太好直接展示給用戶看,所以統(tǒng)一返回SERVER_ERROR代表的異常信息。
# 異于常人的404


spring.mvc.throw-exception-if-no-handler-found=truespring.resources.add-mappings=false
# 統(tǒng)一返回結果
# 驗證統(tǒng)一異常處理
# 主要代碼
@Servicepublic class LicenceService extends ServiceImpl<LicenceMapper, Licence> {private OrganizationClient organizationClient;/*** 查詢{@link Licence} 詳情* @param licenceId* @return*/public LicenceDTO queryDetail(Long licenceId) {Licence licence = this.getById(licenceId);checkNotNull(licence);OrganizationDTO org = ClientUtil.execute(() -> organizationClient.getOrganization(licence.getOrganizationId()));return toLicenceDTO(licence, org);}/*** 分頁獲取* @param licenceParam 分頁查詢參數(shù)* @return*/public QueryDatagetLicences(LicenceParam licenceParam) {String licenceType = licenceParam.getLicenceType();LicenceTypeEnum licenceTypeEnum = LicenceTypeEnum.parseOfNullable(licenceType);// 斷言, 非空ResponseEnum.BAD_LICENCE_TYPE.assertNotNull(licenceTypeEnum);LambdaQueryWrapperwrapper = new LambdaQueryWrapper<>(); wrapper.eq(Licence::getLicenceType, licenceType);IPagepage = this.page(new QueryPage<>(licenceParam), wrapper); return new QueryData<>(page, this::toSimpleLicenceDTO);}/*** 新增{@link Licence}* @param request 請求體* @return*/(rollbackFor = Throwable.class)public LicenceAddRespData addLicence(LicenceAddRequest request) {Licence licence = new Licence();licence.setOrganizationId(request.getOrganizationId());licence.setLicenceType(request.getLicenceType());licence.setProductName(request.getProductName());licence.setLicenceMax(request.getLicenceMax());licence.setLicenceAllocated(request.getLicenceAllocated());licence.setComment(request.getComment());this.save(licence);return new LicenceAddRespData(licence.getLicenceId());}/*** entity -> simple dto* @param licence {@link Licence} entity* @return {@link SimpleLicenceDTO}*/private SimpleLicenceDTO toSimpleLicenceDTO(Licence licence) {// 省略}/*** entity -> dto* @param licence {@link Licence} entity* @param org {@link OrganizationDTO}* @return {@link LicenceDTO}*/private LicenceDTO toLicenceDTO(Licence licence, OrganizationDTO org) {// 省略}/*** 校驗{@link Licence}存在* @param licence*/private void checkNotNull(Licence licence) {ResponseEnum.LICENCE_NOT_FOUND.assertNotNull(licence);}}
-- licenceINSERT INTO licence (licence_id, organization_id, licence_type, product_name, licence_max, licence_allocated)VALUES (1, 1, 'user','CustomerPro', 100,5);INSERT INTO licence (licence_id, organization_id, licence_type, product_name, licence_max, licence_allocated)VALUES (2, 1, 'user','suitability-plus', 200,189);INSERT INTO licence (licence_id, organization_id, licence_type, product_name, licence_max, licence_allocated)VALUES (3, 2, 'user','HR-PowerSuite', 100,4);INSERT INTO licence (licence_id, organization_id, licence_type, product_name, licence_max, licence_allocated)VALUES (4, 2, 'core-prod','WildCat Application Gateway', 16,16);-- organizationsINSERT INTO organization (id, name, contact_name, contact_email, contact_phone)VALUES (1, 'customer-crm-co', 'Mark Balster', '[email protected]', '823-555-1212');INSERT INTO organization (id, name, contact_name, contact_email, contact_phone)VALUES (2, 'HR-PowerSuite', 'Doug Drewry','[email protected]', '920-555-1212');
# 開始驗證
捕獲自定義異常



















捕獲未知異常



# 小結
# 擴展


# 總結

4jpublic class GlobalExceptionHandler {/*** 沒有登錄* @param request* @param response* @param e* @return*/(NoLoginException.class)public Object noLoginExceptionHandler(HttpServletRequest request,HttpServletResponse response,Exception e){log.error("[GlobalExceptionHandler][noLoginExceptionHandler] exception",e);JsonResult jsonResult = new JsonResult();jsonResult.setCode(JsonResultCode.NO_LOGIN);jsonResult.setMessage("用戶登錄失效或者登錄超時,請先登錄");return jsonResult;}/*** 業(yè)務異常* @param request* @param response* @param e* @return*/(ServiceException.class)public Object businessExceptionHandler(HttpServletRequest request,HttpServletResponse response,Exception e){log.error("[GlobalExceptionHandler][businessExceptionHandler] exception",e);JsonResult jsonResult = new JsonResult();jsonResult.setCode(JsonResultCode.FAILURE);jsonResult.setMessage("業(yè)務異常,請聯(lián)系管理員");return jsonResult;}/*** 全局異常處理* @param request* @param response* @param e* @return*/(Exception.class)public Object exceptionHandler(HttpServletRequest request,HttpServletResponse response,Exception e){log.error("[GlobalExceptionHandler][exceptionHandler] exception",e);JsonResult jsonResult = new JsonResult();jsonResult.setCode(JsonResultCode.FAILURE);jsonResult.setMessage("系統(tǒng)錯誤,請聯(lián)系管理員");return jsonResult;}}
評論
圖片
表情
