Return Exception Java to Ajax

Asked

Viewed 918 times

1

all right?

I’m working on a project, and the back-end (java), is totally separated from the front-end, and they communicate through REST.

I have a big question about the exceptions. For example, an error occurred in java, Sqlexception, and I wanted to launch the return of it in ajax. To display it on an Alert or modal for the user.

I have the following codes:

Java:

@POST
@Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response adicionar(Produto produto) throws SQLException
{
    return Response.ok().entity(ProdutoService.insert(produto)).build();
}


Public Produto insert(produto){
    try{
        //tenta inserir dados no banco e retorna produto inserido
    }catch(SQLException e){
        // aqui está o meu 'problema'. Como lançar a exception para o ajax?
    }
}

Ajax:

$.ajax({
    type: 'POST',
    url: urlProduto,
    data: dataJson,
    dataType: "json",
    success: function (response) {
         //seu produto foi inserido com sucesso 
    }, 
    complete: function(){
         //atualiza a grid com os dados do produto
    },
    error: function (jqXHR, textStatus, errorThrown) {
         //o que devo fazer aqui???
         //como posso exibir para o usuário 
         //um alert ou mensagem conforme o erro ocorrido 
         //na SQLException do java?
    }
});

  • 2

    Why return Stacktrace to Ajax? Just in catch you send an error message to your page, and Parse it in ajax. Are you using some kind of framework? Struts, Spring mvc? which?

  • @Ighoraugusto I’m not using any framework, and so the manager isn’t even thinking about any framework at the moment. And the idea is this same, it is not necessary to bring the complete Stacktrace to ajax, and only one message, but the interesting thing is that this message originates from Java, for example I could return the e.getMessage() from Exception.

1 answer

1


Erico,

Normally launch the SQLException and in the error: put the following:

error: function(jqXHR, status, error) {
  var err = eval("(" + jqXHR.responseText + ")");
  alert(err.Message);
}

This should display the detailed error sent by the server.

Although it is in . Net, you can check this link for more information:

http://encosia.com/use-jquery-to-catch-and-display-aspnet-ajax-service-errors/

  • Thanks @Alexandrestrevenski, I believe this will be useful in my project, vlw

  • 2

    I would use JSON.parse instead of the eval.

  • 2

    It’s true @bfavaretto, I copied the reference link example (quoted in the reply), but I agree with you, JSON.parse would be better. There is discussion regarding: http://stackoverflow.com/questions/1843343/json-parse-vs-eval

  • All right, I’m gonna do this today and I’m gonna use JSON.parse. @Alexandre, vlw by example even in Asp.net, because at home I like to use the technology to improve myself. As at work I am working a lot with REST, for the first time, together with Java, at home I am creating a particular project in Webapi with Asp.net to learn and practice.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.