// Copyright (c) 2013-present, febit.org. All Rights Reserved.
package org.febit.wit.exceptions;

import java.io.PrintStream;
import java.io.PrintWriter;
import org.febit.wit.Template;
import org.febit.wit.util.ExceptionUtil;
import org.febit.wit.util.ExceptionUtil.PrintStreamOrWriter;

/**
 *
 * @author zqq90
 */
public abstract class TemplateException extends RuntimeException {

    protected boolean isCaused;
    protected Template template;

    public TemplateException(String message) {
        super(message);
    }

    public TemplateException(Throwable cause) {
        this(cause.getMessage(), cause);
    }

    public TemplateException(String message, Throwable cause) {
        super(message, cause);
        if (cause instanceof TemplateException) {
            ((TemplateException) cause).isCaused = true;
        }
    }

    protected abstract void printBody(PrintStreamOrWriter out, String prefix);

    public Template getTemplate() {
        return template;
    }

    public TemplateException setTemplate(Template template) {
        this.template = template;
        return this;
    }

    @Override
    public void printStackTrace(PrintStream out) {
        synchronized (out) {
            printStackTrace(ExceptionUtil.wrap(out));
        }
    }

    @Override
    public void printStackTrace(PrintWriter out) {
        synchronized (out) {
            printStackTrace(ExceptionUtil.wrap(out));
        }
    }

    protected void printStackTrace(PrintStreamOrWriter out) {
        String prefix = isCaused ? "\t" : "";
        out.print(prefix).print(this).print('\n');
        if (this.template != null) {
            out.print(prefix)
                    .print("template: ")
                    .print(this.template.getName())
                    .print('\n');
        }
        printBody(out, prefix);
        Throwable ourCause = getCause();
        if (ourCause != null) {
            out.print(prefix).print("\tCaused by: \n");
            out.printTrace(ourCause);
        }
    }

}

@@ -95,64 +95,8 @@
 s;%0A%0A
-import java.io.PrintStream;%0Aimport java.io.PrintWriter;%0A
 impo
@@ -122,16 +122,16 @@
 mplate;%0A
+
 import o
@@ -229,15 +229,69 @@
 r;%0A%0A
+import java.io.PrintStream;%0Aimport java.io.PrintWriter;%0A%0A
 /**%0A
- *%0A
  * @
@@ -496,29 +496,28 @@
         
-super
+this
 (message
 );%0A    %7D
@@ -508,16 +508,22 @@
 (message
+, null
 );%0A    %7D
@@ -676,32 +676,32 @@
 owable cause) %7B%0A
-
         super(me
@@ -708,24 +708,37 @@
 ssage, cause
+, true, false
 );%0A        i
@@ -1460,23 +1460,21 @@
 %0A%0A    pr
-otec
+iva
 te
-d
  void pr

Update TemplateException, disable fillInStackTrace in constructor