mirror of
https://github.com/PlayEdu/PlayEdu
synced 2025-06-28 16:22:45 +08:00
异常统一处理
This commit is contained in:
parent
ebd77f97d4
commit
c2aff11b22
@ -0,0 +1,22 @@
|
||||
package xyz.playedu.api.controller;
|
||||
|
||||
import org.springframework.web.HttpRequestHandler;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
import xyz.playedu.api.exception.ServiceException;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
|
||||
@RestControllerAdvice
|
||||
public class ExceptionController {
|
||||
|
||||
@ExceptionHandler(Exception.class)
|
||||
public JsonResponse<String> exceptionHandler(Exception e) {
|
||||
return JsonResponse.error("系统错误", 500);
|
||||
}
|
||||
|
||||
@ExceptionHandler(ServiceException.class)
|
||||
public JsonResponse<String> serviceExceptionHandler(ServiceException e) {
|
||||
return JsonResponse.error(e.getMessage(), 1);
|
||||
}
|
||||
|
||||
}
|
@ -5,9 +5,10 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import xyz.playedu.api.domain.AdminUser;
|
||||
import xyz.playedu.api.exception.ServiceException;
|
||||
import xyz.playedu.api.service.impl.AdminUserServiceImpl;
|
||||
import xyz.playedu.api.types.PaginationResult;
|
||||
import xyz.playedu.api.types.ResponseBody;
|
||||
import xyz.playedu.api.types.JsonResponse;
|
||||
|
||||
@RestController
|
||||
public class AdminUserController {
|
||||
@ -16,9 +17,14 @@ public class AdminUserController {
|
||||
private AdminUserServiceImpl adminUserService;
|
||||
|
||||
@GetMapping("/admin/user/index")
|
||||
public ResponseBody<Object> List(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
|
||||
public JsonResponse<Object> List(@RequestParam("page") Integer page, @RequestParam("size") Integer size) {
|
||||
PaginationResult<AdminUser> result = adminUserService.paginate(page, size, null);
|
||||
return ResponseBody.data(result);
|
||||
return JsonResponse.data(result);
|
||||
}
|
||||
|
||||
@GetMapping("/admmin/user/test")
|
||||
public void TestException() throws ServiceException {
|
||||
throw new RuntimeException("我是错误");
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,22 @@
|
||||
package xyz.playedu.api.exception;
|
||||
|
||||
public class ServiceException extends Exception {
|
||||
public ServiceException() {
|
||||
}
|
||||
|
||||
public ServiceException(String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public ServiceException(String message, Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public ServiceException(Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
|
||||
public ServiceException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
|
||||
super(message, cause, enableSuppression, writableStackTrace);
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
package xyz.playedu.api.types;
|
||||
|
||||
public class ResponseBody<T> {
|
||||
public class JsonResponse<T> {
|
||||
|
||||
private Integer code;
|
||||
private String msg;
|
||||
@ -30,21 +30,21 @@ public class ResponseBody<T> {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public ResponseBody(Integer code, String msg, T data) {
|
||||
public JsonResponse(Integer code, String msg, T data) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static ResponseBody<String> success(String msg) {
|
||||
return new ResponseBody<>(0, msg, null);
|
||||
public static JsonResponse<String> success(String msg) {
|
||||
return new JsonResponse<>(0, msg, null);
|
||||
}
|
||||
|
||||
public static ResponseBody<Object> data(Object data) {
|
||||
return new ResponseBody<>(0, "", data);
|
||||
public static JsonResponse<Object> data(Object data) {
|
||||
return new JsonResponse<>(0, "", data);
|
||||
}
|
||||
|
||||
public static ResponseBody<String> error(String msg, Integer code) {
|
||||
return new ResponseBody<>(code, msg, null);
|
||||
public static JsonResponse<String> error(String msg, Integer code) {
|
||||
return new JsonResponse<>(code, msg, null);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user