Groovy web console

subscribe to the feed Subscribe
to this
site

Last step

Published 3 months ago by Sergio Carrasco
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
class Tools {
    static String getEnclosingMethod(Throwable throwable, Class clazz) {
        try {
            def stackTraceElement = org.codehaus.groovy.runtime.StackTraceUtils.sanitize(throwable).stackTrace.find {
                it.className == clazz.name
            }
            if (stackTraceElement)
                return "${stackTraceElement.className}::${stackTraceElement.methodName}"
        } catch (Throwable ignored) {}
        return "unknown"
    }
}

class AAA {
    def hacerAAA(s){
        new BBB().hacerBBB(s)
    }
}

class BBB {
    def hacerBBB(s) {
        new CCC().hacerCCC(s)
    }
}

class CCC {
    def hacerCCC(s) {
        throw new Exception("FUCKING ERROR from ${s}")
    }
}

class Executor {
    def before() {
//        new AAA().hacerAAA("<before>")
    }

    def process() {
//        new AAA().hacerAAA("<process>")
    }

    def after() {
        new AAA().hacerAAA("<after>")
    }

    def init(){
        try {
            before()
            process()
            after()
        } catch (Exception e) {
            println "fallo en => ${Tools.getEnclosingMethod(e, getClass())}"
            e.printStackTrace()
        }
    }
}

new Executor().init()