0

I have

def exception = request.exception.stackTraceLines

in Groovy controller. How can I get the value of the exception in the JavaScript.

3 Answers 3

1

If you are adding exception to your return like this.

flash.message = message(exception: 'Error: xxx');

you can get it like this

<div class="message" role="status"> ${flash.message} </div>

just use ${ your flash.your_var_name}

Sign up to request clarification or add additional context in comments.

Comments

1

You have multiple options to fix this. When using javascript I usually use a wrapper which can hold the error message/stacktrace. It depends on where and how you want to do the error handling. Example:

def book = new Book(params)
book.validate()

render (contentType: "text/json") {[
    "data": book,
    "errors": book.hasErrors() ? book.errors : null
]}

Then you can check if "errors" has a value when getting back your JSON to determine if there are errors in the input for instance. (Elvis operator probably works too, book.errors ?: null) Other (uncatched) exceptions I handle in the error callback I usually define in my JavaScript. (jQuery mostly, (with malsup jquery.form.js in this case))

$(function() {
  $("form").live("submit", function() {
    $(this).ajaxSubmit({
      error: function (msg) {
        /* Catch hard errors here (500's) */
        alert("Error occurred: " + msg);
      },
      success: function(wrapper) {
         if (wrapper.errors != null) {
            /* Handle model errors here */
         } else {
            /* Parse data here */
            var book = wrapper.data;
         }
      }
    });
    return false;
});

Comments

0

You can also do this

render(template:"book",model:[book:theBook, exception:exception])

Or like this (didnt try, don't know if works)

render(template:"book",model:[book:theBook, exception:exception!=null?exception:""])

and then access from the GSP like this

${exception.getMessage()}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.