Rohit Bajaj
asked on
Handling exception in javascript ajax call
HI,
I have the following js code :
And the exception handler on my server side is :
Now all this exception will get caught in ajax call error function. But i want to do different things when there is
CannotGetJdbcConnectionExc eption and handle differently when there is no special exception but the exception is from @ExceptionHandler(Exceptio n.class)
public ResponseEntity<String> handleException(Exception e) {
How should i differentiate both these in ajax error function.. should i create a code for CannotGetJdbcConnectionExc eption and Exception and pass it to error function so that i can handle it differently.
Basically if there is CannotGetJdbcConnectionExc eption i want to show a certain message to the user on my web app and if its Exception i want to show a different message and also change some css.
Thanks
I have the following js code :
$.ajax({
contentType: "application/json; charset=utf-8",
url : createURL,
type: "POST",
data : JSON.stringify(objectTobeSent),
success: function(snippet, status, xhr) {
if (xhr.status !== 201) {
alert("Couldn't create code snippet. Please try after sometime");
} else if (!utils.sendMessage(snippet, objectTobeSent.title, $("#message").val())) {
window.location.href = snippet.URL;
}
},
error: function(request, status, error) {
$('.error_message').show();
}
});
});
And the exception handler on my server side is :
public abstract class BaseController {
private static final Logger logger = LoggerFactory.getLogger(BaseController.class);
@ExceptionHandler(EmptyResultDataAccessException.class)
public ResponseEntity<String> handleServerException(EmptyResultDataAccessException e) {
String message = "Page not found";
logger.error(message, e);
return new ResponseEntity<String>(message, HttpStatus.NOT_FOUND);
}
@ExceptionHandler(CannotGetJdbcConnectionException.class)
public ResponseEntity<String> handleNoConnection(CannotGetJdbcConnectionException e) {
String message = "Unable to connect to the database. Is it running?";
logger.error(message, e);
return new ResponseEntity<String>(message, HttpStatus.INTERNAL_SERVER_ERROR);
}
@ExceptionHandler(Exception.class)
public ResponseEntity<String> handleException(Exception e) {
String message = "An unexpected error occurred";
logger.error(message, e);
return new ResponseEntity<String>(message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
Now all this exception will get caught in ajax call error function. But i want to do different things when there is
CannotGetJdbcConnectionExc
public ResponseEntity<String> handleException(Exception e) {
How should i differentiate both these in ajax error function.. should i create a code for CannotGetJdbcConnectionExc
Basically if there is CannotGetJdbcConnectionExc
Thanks
In the past i've just created a error code that the ajax error handler inspects.
ASKER
HI,
I am using the following code to handle the error now :
I am doing this because i want to capture the exception which happens because the data is too large to contain in the mysql database column.So i did like above : if(e.getCause() instanceof MysqlDataTruncation && ((MysqlDataTruncation) e.getCause()).getErrorCode ()==1406){
what do you think about this.. is this a correct way.
As i am just getting DataIntegrityViolationExce ption from spring. I dont know whether it really happened because of a large data... I found this error code in a test run when i really had a large data...
I am using the following code to handle the error now :
@ExceptionHandler(DataIntegrityViolationException.class)
public ResponseEntity<String> handleIntegrityException(DataIntegrityViolationException e) {
if(e.getCause() instanceof MysqlDataTruncation && ((MysqlDataTruncation) e.getCause()).getErrorCode()==1406){
String message = "Code snippet too large";
logger.error(message, e);
return new ResponseEntity<String>(message, HttpStatus.PAYLOAD_TOO_LARGE);
}
else {
String message = "An unexpected error occurred";
logger.error(message, e);
return new ResponseEntity<String>(message, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
I am doing this because i want to capture the exception which happens because the data is too large to contain in the mysql database column.So i did like above : if(e.getCause() instanceof MysqlDataTruncation && ((MysqlDataTruncation) e.getCause()).getErrorCode
what do you think about this.. is this a correct way.
As i am just getting DataIntegrityViolationExce
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
hi,
can u point some link related to java.validation that u are talking about. on a google search i found something like -
http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html
also in my case i am using jdbctemplate to insert or query data
can u point some link related to java.validation that u are talking about. on a google search i found something like -
http://docs.oracle.com/javaee/6/tutorial/doc/gircz.html
also in my case i am using jdbctemplate to insert or query data
Doesn't matter about jdbc template.
Lookup hibernate validator.
Lookup hibernate validator.
ASKER
HI,
As per what i understood is -
I have a snippet class containing
String text;
This class object is inserted into the table...
Now the column for text is of type TEXT which has some limit of 65,535 characters.
So in order to use hibernate validation what i intend to do is
@Size(max = 65535)
String text;
And then do something like validate(snippet) before inserting the data into the table.
So in this case i am not even causing the exception to happen and handling it before that .
As per what i understood is -
I have a snippet class containing
String text;
This class object is inserted into the table...
Now the column for text is of type TEXT which has some limit of 65,535 characters.
So in order to use hibernate validation what i intend to do is
@Size(max = 65535)
String text;
And then do something like validate(snippet) before inserting the data into the table.
So in this case i am not even causing the exception to happen and handling it before that .
Yes sort of. You can manually call validate or let the server do it for you. Depends if your server supports jax-rs validation.