sa-token-solon-plugin:调整示例代码关于异常的处理

This commit is contained in:
noear 2022-11-15 22:46:22 +08:00
parent 062af081f7
commit 1e2abfaa15

View File

@ -5,8 +5,9 @@ import com.pj.util.AjaxJson;
import cn.dev33.satoken.exception.*; import cn.dev33.satoken.exception.*;
import org.noear.solon.annotation.Component; import org.noear.solon.annotation.Component;
import org.noear.solon.core.event.EventListener;
import org.noear.solon.core.handle.Context; import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Filter;
import org.noear.solon.core.handle.FilterChain;
/** /**
@ -15,13 +16,12 @@ import org.noear.solon.core.handle.Context;
* @author noear * @author noear
*/ */
@Component @Component
public class GlobalException implements EventListener<Throwable> { public class GlobalExceptionFilter implements Filter {
@Override @Override
public void onEvent(Throwable e) { public void doFilter(Context ctx, FilterChain chain) throws Throwable {
Context c = Context.current(); try {
chain.doFilter(ctx);
if (c != null) { } catch (SaTokenException e) {
// 不同异常返回不同状态码 // 不同异常返回不同状态码
AjaxJson aj = null; AjaxJson aj = null;
if (e instanceof NotLoginException) { // 如果是未登录异常 if (e instanceof NotLoginException) { // 如果是未登录异常
@ -40,7 +40,7 @@ public class GlobalException implements EventListener<Throwable> {
aj = AjaxJson.getError(e.getMessage()); aj = AjaxJson.getError(e.getMessage());
} }
c.result = aj; ctx.render(aj);
} }
} }
} }