NutzCN Logo
问答 运行 系统异常 如何捕获 封装 返回对应的json信息呢
发布于 2033天前 作者 Hamming 1603 次浏览 复制 上一个帖子 下一个帖子
标签:

运行 系统异常 如何捕获 封装 返回对应的json信息呢
例如 查询 传参为空 查询 抛异常如下 返回 就没有任何数据
java.lang.IllegalArgumentException
如何捕捉异常 返回 对应的json信息呢

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);
    }
}

nutzboot 如何配置 error处理呢

@ChainBy依然是可用的

呃 如何修改呢

@IocBy(args="*slog")

slog是aop的, 你不是要改动作链吗?

编译错误

@ChainBy(type= ErrorProcessor.class, args={})
Error:(45, 30) java: 不兼容的类型: java.lang.Class<io.nutz.nutzsite.common.processor.ErrorProcessor>无法转换为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);
    }
}

把文档看一看...

添加回复
请先登陆
回到顶部