新增权限管理相关接口
This commit is contained in:
@@ -45,6 +45,15 @@ public class CommonResult<T> {
|
||||
return new CommonResult<T>(errorCode.getCode(), errorCode.getMessage(), null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败返回结果
|
||||
* @param errorCode 错误码
|
||||
* @param message 错误信息
|
||||
*/
|
||||
public static <T> CommonResult<T> failed(IErrorCode errorCode,String message) {
|
||||
return new CommonResult<T>(errorCode.getCode(), message, null);
|
||||
}
|
||||
|
||||
/**
|
||||
* 失败返回结果
|
||||
* @param message 提示信息
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.macro.mall.common.exception;
|
||||
|
||||
import com.macro.mall.common.api.IErrorCode;
|
||||
|
||||
/**
|
||||
* 自定义API异常
|
||||
* Created by macro on 2020/2/27.
|
||||
*/
|
||||
public class ApiException extends RuntimeException {
|
||||
private IErrorCode errorCode;
|
||||
|
||||
public ApiException(IErrorCode errorCode) {
|
||||
super(errorCode.getMessage());
|
||||
this.errorCode = errorCode;
|
||||
}
|
||||
|
||||
public ApiException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ApiException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ApiException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public IErrorCode getErrorCode() {
|
||||
return errorCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package com.macro.mall.common.exception;
|
||||
|
||||
import com.macro.mall.common.api.IErrorCode;
|
||||
|
||||
/**
|
||||
* 断言处理类,用于抛出各种API异常
|
||||
* Created by macro on 2020/2/27.
|
||||
*/
|
||||
public class Asserts {
|
||||
public static void fail(String message) {
|
||||
throw new ApiException(message);
|
||||
}
|
||||
|
||||
public static void fail(IErrorCode errorCode) {
|
||||
throw new ApiException(errorCode);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.macro.mall.common.exception;
|
||||
|
||||
import com.macro.mall.common.api.CommonResult;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
|
||||
/**
|
||||
* 全局异常处理
|
||||
* Created by macro on 2020/2/27.
|
||||
*/
|
||||
@ControllerAdvice
|
||||
public class GlobalExceptionHandler {
|
||||
|
||||
@ResponseBody
|
||||
@ExceptionHandler(value = ApiException.class)
|
||||
public CommonResult handle(ApiException e) {
|
||||
if (e.getErrorCode() != null) {
|
||||
return CommonResult.failed(e.getErrorCode());
|
||||
}
|
||||
return CommonResult.failed(e.getMessage());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user