user2427
user2427

Reputation: 7932

how to get detailed information about execution of a script in groovy

I found here a very good example of what I want:

enter image description here

Basically to be able to execute a String as a groovy script with an expression, but if the condition is false, I want to show detailed information about why it was evaluated as false.

EDIT

I want an utility method that work like this:

def expression = "model.book.title == \"The Shining\""
def output = magicMethod(expression)

// output.result: the exact result of executing expression
// output.detail: could be a string telling me why this expression returns true or false, similar to de image

I think it may be a combination of Eval.me + assert and to catch the exception in order to get details

Upvotes: 1

Views: 155

Answers (1)

user2427
user2427

Reputation: 7932

Yeah, it works with assert, thanks for the idea @Justin Piper

here is the snippet:

def model = [model:[book:[title:"The Shinning"]]]

def magicMethod= { String exp ->
    def out = [:]
    out.result = Eval.x(model,"x.with{${exp}}")
    try{
        if(out.result){
            Eval.x(model,"x.with{!assert ${exp}}")
        }else{
            Eval.x(model,"x.with{assert ${exp}}")
        }
    }catch(Throwable e){
        out.detail = e.getMessage()
    }
    return out
}


def expression = "model.book.title == \"The Shining\""
def output = magicMethod(expression)

println "result: ${output.result}"
println "detail: ${output.detail}"

Upvotes: 1

Related Questions