Win a copy of OCP Java SE 8 Programmer II Exam Study Guide this week in the OCP forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to pass the translated error message to the front-end?  RSS feed

 
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Created @ControllerAdvice and caught few exceptions. ex: FileNotFoundException or User Defined exception
2. Translated the error message - error message + parameters and constructed the proper error message.

Now not sure how to send the response to the front-end

Currently 500 error is thrown. I have kept the return type as user defined object.

Someone please advise how to gracefully throw an error message to the front-end so that it will be displayed to the user without any issues.

 
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can use ModelAndView object. set the error message  and return ModelAndView object as response.

Please check below code. It helps.


write this piece of code inside @ControllerAdvice annotation class.
 
Kathir jeyap
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I used the same code...However 500 error is returned and the page got redirected to the error page

I want the same page or window or dialog should be retained..

Please advise.
 
vipul John
Ranch Hand
Posts: 245
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Kathir jeyap wrote:
I used the same code...However 500 error is returned and the page got redirected to the error page

I want the same page or window or dialog should be retained..

Please advise.



Why 500 is returned. Could you post the code you've written or the exception that is thrown?
 
Kathir jeyap
Ranch Hand
Posts: 38
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see in the javascript in ajax call we have only success case and inside the success case we have differentiated as success or error based on the response type

If we add the failure in ajax script and handle this will it be handled ??

What should be the right way in the front-end for ajax calls ???
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!