13 回复
定义了这个类 怎么捕获异常 返回信息呢 例如:java.lang.ExceptionInInitializerError
package io.nutz.nutzsite.common.processor;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.impl.processor.AbstractProcessor;
/**
* @Author: Haimming
* @Date: 2019-07-03 09:57
* @Version 1.0
*/
@IocBean(singleton=false)
public class ErrorProcessor extends AbstractProcessor {
@Override
public void process(ActionContext ac) throws Throwable {
}
}
public class MyFailProcessor extends ViewProcessor {
private static final Log log = Logs.get();
@Override
public void init(NutConfig config, ActionInfo ai) throws Throwable {
view = evalView(config, ai, ai.getFailView());
}
@Override
public void process(ActionContext ac) throws Throwable {
if (log.isWarnEnabled()) {
String uri = Mvcs.getRequestPath(ac.getRequest());
if (ac.getError() instanceof MvcException) {
log.warn(String.format("Error@%s: %s", uri, ac.getError().getMessage()));
} else {
log.warn(String.format("Error@%s :", uri), ac.getError());
}
}
ac.setMethodReturn(Response.fail(ac.getError()));
super.process(ac);
}
}
MvcException 找不到
Response.fail 找不到
自定义的异常处理类 如何配置让它生效呢
或者 替换FailProcessor
package io.nutz.nutzsite.common.processor;
import io.nutz.nutzsite.common.base.Result;
import org.nutz.integration.shiro.NutShiro;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.impl.processor.ViewProcessor;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
/**
* @Author: Haimming
* @Date: 2019-07-03 09:57
* @Version 1.0
*/
public class ErrorProcessor extends ViewProcessor {
private static final Log log = Logs.get();
public static boolean isAjax(ServletRequest req) {
String value = ((HttpServletRequest)req).getHeader("X-Requested-With");
return value != null && "XMLHttpRequest".equalsIgnoreCase(value.trim());
}
@Override
public void init(NutConfig config, ActionInfo ai) throws Throwable {
view = evalView(config, ai, ai.getFailView());
}
@Override
public void process(ActionContext ac) throws Throwable {
if (log.isWarnEnabled()) {
String uri = Mvcs.getRequestPath(ac.getRequest());
if (ac.getError() instanceof NullPointerException) {
}
// log.warn(String.format("Error@%s: %s", uri, ac.getError().getMessage()));
// } else {
// log.warn(String.format("Error@%s :", uri), ac.getError());
// }
}
if (isAjax(ac.getRequest())) {
// ac.getResponse().addHeader("loginStatus", "accessDenied");
NutShiro.rendAjaxResp(ac.getRequest(), ac.getResponse(), Result.error("系统异常"));
} else {
// new ServerRedirectView(500).render(ac.getRequest(), ac.getResponse(), null);
}
super.process(ac);
}
}
@ChainBy依然是可用的
呃 如何修改呢
@IocBy(args="*slog")
slog是aop的, 你不是要改动作链吗?
编译错误
@ChainBy(type= ErrorProcessor.class, args={})
Error:(45, 30) java: 不兼容的类型: java.lang.Class无法转换为java.lang.Class extends org.nutz.mvc.ActionChainMaker>
package io.nutz.nutzsite.common.processor;
import io.nutz.nutzsite.common.base.Result;
import org.nutz.integration.shiro.NutShiro;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.Mvcs;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.impl.processor.ViewProcessor;
import javax.servlet.ServletRequest;
import javax.servlet.http.HttpServletRequest;
/**
* @Author: Haimming
* @Date: 2019-07-03 09:57
* @Version 1.0
*/
@IocBean(singleton = false)
public class ErrorProcessor extends ViewProcessor {
private static final Log log = Logs.get();
public static boolean isAjax(ServletRequest req) {
String value = ((HttpServletRequest)req).getHeader("X-Requested-With");
return value != null && "XMLHttpRequest".equalsIgnoreCase(value.trim());
}
@Override
public void init(NutConfig config, ActionInfo ai) throws Throwable {
view = evalView(config, ai, ai.getFailView());
}
@Override
public void process(ActionContext ac) throws Throwable {
if (log.isWarnEnabled()) {
String uri = Mvcs.getRequestPath(ac.getRequest());
// if (ac.getError() instanceof MvcException) {
// log.warn(String.format("Error@%s: %s", uri, ac.getError().getMessage()));
// } else {
// log.warn(String.format("Error@%s :", uri), ac.getError());
// }
}
if (isAjax(ac.getRequest())) {
// ac.getResponse().addHeader("loginStatus", "accessDenied");
NutShiro.rendAjaxResp(ac.getRequest(), ac.getResponse(), Result.error("系统异常"));
} else {
// new ServerRedirectView(500).render(ac.getRequest(), ac.getResponse(), null);
}
super.process(ac);
}
}