ErrorHandler.java
1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.myxrk.rbac.aspect;
import com.myxrk.rbac.exception.BaseException;
import com.myxrk.rbac.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;
@Slf4j
@RestControllerAdvice
public class ErrorHandler {
@ExceptionHandler(Throwable.class)
public Result<Object> handleException(Exception e) {
log.error("Unknown Exception was thrown!", e);
return Result.error(500, e.getMessage());
}
@ExceptionHandler(BaseException.class)
public Result<Object> handleCustomException(BaseException e) {
log.warn("Biz Exception was thrown!", e);
return Result.error(e.getCode(), e.getMessage());
}
@ExceptionHandler(AuthenticationException.class)
@ResponseBody
public Result<String> handleException(AuthenticationException e) {
log.warn("AuthenticationException was thrown!", e);
return Result.error(601, "授权失败");
}
@ExceptionHandler(AuthorizationException.class)
@ResponseBody
public Result<String> handleException(AuthorizationException e) {
log.warn("AuthorizationException was thrown!", e);
return Result.error(631, "授权失败");
}
}