Search in sources :

Example 1 with ErrorInfo

use of org.beetl.core.exception.ErrorInfo in project beetl2.0 by javamonkey.

the class ConsoleErrorHandler method processExcption.

@Override
public void processExcption(BeetlException ex, Writer writer) {
    ErrorInfo error = new ErrorInfo(ex);
    if (error.getErrorCode().equals(BeetlException.CLIENT_IO_ERROR_ERROR)) {
        // 不输出详细提示信息
        if (!ex.gt.conf.isIgnoreClientIOError) {
            println(writer, "客户端IO异常:" + getResourceName(ex.resource.id) + ":" + error.getMsg());
            if (ex.getCause() != null) {
                this.printThrowable(writer, ex.getCause());
            }
            return;
        }
    }
    int line = error.getErrorTokenLine();
    StringBuilder sb = new StringBuilder(">>").append(this.getDateTime()).append(":").append(error.getType()).append(":").append(error.getErrorTokenText()).append(" 位于").append(line).append("行").append(" 资源:").append(getResourceName(ex.resource.id));
    if (error.getErrorCode().equals(BeetlException.TEMPLATE_LOAD_ERROR)) {
        if (error.getMsg() != null)
            sb.append(error.getMsg());
        println(writer, sb.toString());
        println(writer, ex.gt.getResourceLoader().getInfo());
        return;
    }
    println(writer, sb.toString());
    if (ex.getMessage() != null) {
        println(writer, ex.getMessage());
    }
    ResourceLoader resLoader = ex.gt.getResourceLoader();
    // 潜在问题,此时可能得到是一个新的模板(开发模式下),不过可能性很小,忽略!
    String content = null;
    try {
        Resource res = ex.resource;
        // 显示前后三行的内容
        int[] range = this.getRange(line);
        content = res.getContent(range[0], range[1]);
        if (content != null) {
            String[] strs = content.split(ex.cr);
            int lineNumber = range[0];
            for (int i = 0; i < strs.length; i++) {
                print(writer, "" + lineNumber);
                print(writer, "|");
                println(writer, strs[i]);
                lineNumber++;
            }
        }
    } catch (IOException e) {
    // ingore
    }
    if (error.hasCallStack()) {
        println(writer, "  ========================");
        println(writer, "  调用栈:");
        for (int i = 0; i < error.getResourceCallStack().size(); i++) {
            println(writer, "  " + error.getResourceCallStack().get(i) + " 行:" + error.getTokenCallStack().get(i).line);
        }
    }
    printCause(error, writer);
    try {
        writer.flush();
    } catch (IOException e) {
    }
}
Also used : ErrorInfo(org.beetl.core.exception.ErrorInfo) IOException(java.io.IOException)

Example 2 with ErrorInfo

use of org.beetl.core.exception.ErrorInfo in project beetl2.0 by javamonkey.

the class WebErrorHandler method processExcption.

@Override
public void processExcption(BeetlException e, Writer writer) {
    // 判断是不是开发者模式,如果不是调用父类方法(默认输出控制台)
    if (!Boolean.valueOf(e.gt.getConf().getProperty("RESOURCE.autoCheck"))) {
        super.processExcption(e, writer);
    }
    ErrorInfo error = new ErrorInfo(e);
    StringBuilder title = new StringBuilder();
    StringBuilder msg = new StringBuilder();
    if (error.getErrorCode().equals(BeetlException.CLIENT_IO_ERROR_ERROR)) {
        // 不输出详细提示信息
        title = new StringBuilder(">>").append("客户端IO异常:").append(e.resource.getId());
        if (e.getCause() != null) {
            msg.append(e.getCause());
        }
        render(writer, title.toString(), msg.toString());
        return;
    }
    int line = error.getErrorTokenLine();
    title = new StringBuilder(">>").append(error.getType()).append(":").append(error.getErrorTokenText()).append(" 位于").append(line).append("行").append(" 资源:").append(e.resource.getId());
    if (error.getErrorCode().equals(BeetlException.TEMPLATE_LOAD_ERROR)) {
        if (error.getMsg() != null)
            msg.append(error.getMsg());
        render(writer, title.toString(), msg.toString());
        return;
    }
    if (e.getMessage() != null) {
        msg.append(e.getMessage()).append("\n");
    }
    ResourceLoader resLoader = e.gt.getResourceLoader();
    // 潜在问题,此时可能得到是一个新的模板,不过可能性很小,忽略!
    String content = null;
    Resource res = resLoader.getResource(e.resource.getId());
    // 显示前后三行的内容
    int[] range = this.getRange(line);
    try {
        content = res.getContent(range[0], range[1]);
    } catch (IOException e1) {
    }
    if (content != null) {
        String[] strs = content.split(e.cr);
        int lineNumber = range[0];
        for (int i = 0; i < strs.length; i++) {
            msg.append("" + lineNumber).append("|").append(strs[i].trim()).append("\n");
            lineNumber++;
        }
    }
    if (error.hasCallStack()) {
        msg.append("  ========================").append("\n");
        msg.append("  调用栈:").append("\n");
        for (int i = 0; i < error.getResourceCallStack().size(); i++) {
            msg.append("  " + error.getResourceCallStack().get(i) + " 行:").append(error.getTokenCallStack().get(i).line).append("\n");
        }
        Throwable t = error.getCause();
        if (t != null) {
            msg.append(t.toString()).append("\n");
        }
    }
    render(writer, title.toString(), msg.toString());
    try {
        writer.flush();
    } catch (IOException e1) {
    }
}
Also used : ResourceLoader(org.beetl.core.ResourceLoader) ErrorInfo(org.beetl.core.exception.ErrorInfo) Resource(org.beetl.core.Resource) IOException(java.io.IOException)

Aggregations

IOException (java.io.IOException)2 ErrorInfo (org.beetl.core.exception.ErrorInfo)2 Resource (org.beetl.core.Resource)1 ResourceLoader (org.beetl.core.ResourceLoader)1