Software Programmer
asked on
@ControllerAdvice and Error Construction in Business Layer
Assume i have defined @ControllerAdvice
@ControllerAdvice
public class ErrorHandler {
// example
@ExceptionHandler(ParseExc eption.cla ss)
public void handleParseException(Parse Exception exception, HttpServletResponse response) {
// return response - error message, error description, error type (ERROR or EXCEPTION)
}
}
The problem is where i will formulate the error message text with parameters and also the message type - ERROR or EXCEPTION
Example, Assume file not found exception is thrown for the given file name
Generally, in message.properties file we will be having text as File Doesn't Exist for file {0}
The translation of error message usually happens in the presentation layer.....
Now if we need to pass the error message so that controller advice takes care of passing it to the UI....
Do we need to construct the error message in the service layer before sending ??? Where the exception and the parameters will be binded ???
for example
public void accessFile(String fileName) {
File file = new File(fileName);
if(!file.exists()) {
throw new FileNotFoundException(Cons tants.FILE _NOT_FOUND .....);
How to construct the error message with property key and sending with proper error message binded with exception???
so that in controller advice we would just use exception.getMessage() which will have the translated text.
}
}
Please let me know how to do it.
@ControllerAdvice
public class ErrorHandler {
// example
@ExceptionHandler(ParseExc
public void handleParseException(Parse
// return response - error message, error description, error type (ERROR or EXCEPTION)
}
}
The problem is where i will formulate the error message text with parameters and also the message type - ERROR or EXCEPTION
Example, Assume file not found exception is thrown for the given file name
Generally, in message.properties file we will be having text as File Doesn't Exist for file {0}
The translation of error message usually happens in the presentation layer.....
Now if we need to pass the error message so that controller advice takes care of passing it to the UI....
Do we need to construct the error message in the service layer before sending ??? Where the exception and the parameters will be binded ???
for example
public void accessFile(String fileName) {
File file = new File(fileName);
if(!file.exists()) {
throw new FileNotFoundException(Cons
How to construct the error message with property key and sending with proper error message binded with exception???
so that in controller advice we would just use exception.getMessage() which will have the translated text.
}
}
Please let me know how to do it.
ASKER
My problem is where to translate the message. Some says since i18n needs to be supported. It is always translate the message in the presentation layer instead of the backend.
I am planning to send the Constants.FILE_NOT_FOUND key string as well as the parameters as Object[] so that @ControllerAdvice translates using MessageSource and sends the text.
Is that would be a right approach ???
also, i would like to know how to send the response back to the front-end...When i catch the exception and sent a pojo object to the client.it says 500 error.
Do we need to set any status code to the response object ??? Basically i want the exception to be thrown to the user in the same dialog.
Assume a user submit a form and the exception should be shown in the dialog in the same form instead of re-directing to the error page.
I am planning to send the Constants.FILE_NOT_FOUND key string as well as the parameters as Object[] so that @ControllerAdvice translates using MessageSource and sends the text.
Is that would be a right approach ???
also, i would like to know how to send the response back to the front-end...When i catch the exception and sent a pojo object to the client.it says 500 error.
Do we need to set any status code to the response object ??? Basically i want the exception to be thrown to the user in the same dialog.
Assume a user submit a form and the exception should be shown in the dialog in the same form instead of re-directing to the error page.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am not sure returning the status..Can we have as below
a. always return the response status as "SUCCESS"
b. send a reponse type a string variable as "ERROR" or "SUCCESS"
c. let AJAX takes the decision based on the response type
Please let me know what you say ???
a. always return the response status as "SUCCESS"
b. send a reponse type a string variable as "ERROR" or "SUCCESS"
c. let AJAX takes the decision based on the response type
Please let me know what you say ???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
We have something like below....Please let me know whether this is the correct approach or wrong...Help me with the code snippet.
jQuery.ajax({
type:"post",
dataType:"json",
url: myAjax.ajaxurl,
data: {action: 'submit_data', info: info},
success: function(data) {
data.response.type == 'success' {
// success dialog
}
data.response.type == 'failure' {
// failure dialog
}
}
});
jQuery.ajax({
type:"post",
dataType:"json",
url: myAjax.ajaxurl,
data: {action: 'submit_data', info: info},
success: function(data) {
data.response.type == 'success' {
// success dialog
}
data.response.type == 'failure' {
// failure dialog
}
}
});
Yes this is correct.
ASKER
Remember it doesn't have error function...Some says it is not the best practice...
Do we need to have error function or not ???
We have redundant code in the back-end...
Please analyse and comment..
Do we need to have error function or not ???
We have redundant code in the back-end...
Please analyse and comment..
What do you mean by "error function"?
ASKER
success: function(data) {
.....
error:function(data)
.....
error:function(data)
I don't know how the error function is mapped on the front end. Is it called when it gets an error code (an HTTP status code starting with 5xx)?
ASKER
Not sure..i will check and get back to you
Both are valid comments.
Why not just do
Open in new window