@ControllerAdvice and Error Construction in Business Layer

Assume i have defined @ControllerAdvice

public class ErrorHandler {
     // example
    public void handleParseException(ParseException 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(Constants.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.
Software ProgrammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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.

Why not just do

if(!file.exists()) { 
   String msg = Properties.getMessage(Constants.FILE_NOT_FOUND);
   // then format the msg and replace the placeholder with the fileName
   // then throw an exception.
       throw new FileNotFoundException(msg); 

Open in new window

Software ProgrammerAuthor Commented:
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.
Normally you wouldn't need to translate the message. You should have a resource bundle for each locale you want to support and let the JVM do it for you.

If you want to return a different HTTP status to the user you can do it by declaring the status with the @ResponseStatus annotation.
Exploring SQL Server 2016: Fundamentals

Learn the fundamentals of Microsoft SQL Server, a relational database management system that stores and retrieves data when requested by other software applications.

Software ProgrammerAuthor Commented:
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 ???
You should only send SUCCESS if the operation is successful with the appropriate success status code. When an error happens you should return an error object with the appropriate status error status (404, 503, etc).

Then Ajax should decide on the response status, not on the response type.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Software ProgrammerAuthor Commented:
We have something like below....Please let me know whether this is the correct approach or wrong...Help me with the code snippet.

    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.
Software ProgrammerAuthor Commented:
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..
What do you mean by "error function"?
Software ProgrammerAuthor Commented:
success: 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)?
Software ProgrammerAuthor Commented:
Not sure..i will check and get back to you
Both are valid comments.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.