package com.tinkerpop.gremlin.console;

import groovy.lang.Closure;
import org.codehaus.groovy.tools.shell.IO;

/**
 * @author Marko A. Rodriguez (http://markorodriguez.com)
 */
public class ErrorHookClosure extends Closure {

    private final IO io;

    public ErrorHookClosure(final Object owner, final IO io) {
        super(owner);
        this.io = io;
    }

    public Object call(final Object[] args) {
        if (args.length > 0) {
            try {
                final Throwable e = (Throwable) args[0];
                String message = e.getMessage();
                if (null != message) {
                    message = message.replace("startup failed:", "");
                    io.err.println(message.trim());
                } else {
                    io.err.println(e);
                }
                return null;
            } catch (Exception e) {
                io.err.println("An undefined error has occurred: " + args[0]);
                return null;
            }
        } else {
            io.err.println("An undefined error has occurred");
            return null;
        }
    }
}

@@ -105,16 +105,48 @@
 ll.IO;%0A%0A
+import java.io.BufferedReader;%0A%0A
 /**%0A * @
@@ -828,32 +828,475 @@
               %7D%0A
+%0A                io.err.print(%22Display stack trace? %5ByN%5D %22);%0A                io.err.flush();%0A                String line = new BufferedReader(io.in).readLine();%0A                if (null == line)%0A                    line = %22%22;%0A                io.err.print(line.trim());%0A                io.err.println();%0A                if (line.trim().equals(%22y%22) %7C%7C line.trim().equals(%22Y%22)) %7B%0A                    e.printStackTrace(io.err);%0A                %7D%0A%0A
                 

work on error hook to ask the user if they want to see a stack trace.