Can we use Model addAttribute to handle errors in the UI ?

Example Sample Code
@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView addEmployee(Model model, EmpTO empTO, BindingResult bindingResult){
    try {
        empService.saveOrUpdateEmployee(empTO);
    } catch (Exception e) {
        if (e instanceof InvException) {
            model.addAttribute("error", ((InvException) e).getError());
        } else {
            model.addAttribute("error", PresentationUtil.handleApplicationRuntimeError(e));
        }
        return new ModelAndView("emp/save");
    }
    empTO = new EmpTO();
    return new ModelAndView("emp/home");
}

Open in new window


Questions:
---------------

1. Is the above is the correct approach?
2. Can we store the error code and description in a DB table or hard-coded string and let UI handles the error codes?
3. Is this is a standard industry practice?

Thanks
Software ProgrammerAsked:
Who is Participating?
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.

girionisCommented:
1. Is the above is the correct approach?

This is one correct approach.

2. Can we store the error code and description in a DB table or hard-coded string and let UI handles the error codes?

You can. Of course the UI should have some logic to handle each different error code.

3. Is this is a standard industry practice?

It's hard to say if this is the standard. There other ways to do it, which I personally think it is easier and provide more readability. When I need to handle exceptions I usually define an exception handler

@ExceptionHandler(InvException.class)
 public ModelAndView handleInvException(InvException ex) {
  Map<String, InvException> model = new HashMap<String, InvException>();
  model.put("exception", ex);
  return new ModelAndView("error", model);

 }

Open in new window


But of course all this depends on the use case.
0
Software ProgrammerAuthor Commented:
I like your way via exception handler. what are things needs to be covered in an exception handler or handling exceptions via controller advice?

Questions

1. Do we go for exception handler or controller advice to log the exception so that the error will be logged in application log instead of tomcat log ???
2. What are the benefits of handling via exception handler or controller advice ???
0
girionisCommented:
An @ExceptionHandler is an annotation defined in a @ControllerAdvice. The two of them are tied together.

@ControllerAdvice
public class ExceptionConfiguration {

    @ResponseStatus(HttpStatus.CONFLICT)
    @ExceptionHandler(RuntimeException.class)
    public void handleRuntimeException() {}

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(CustomException.class)
    public void handleCustomException() {}

}

Open in new window

0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Software ProgrammerAuthor Commented:
Can you send me the full code of anywhere where controller advice is implemented???  Template is being shown in many web-pages and couldn't understand the full purpose of it. I couldn't understand how it solves the problem. Any code in detail would be very much appreciated.
0
Software ProgrammerAuthor Commented:
These links i have already read...I need a code from a project or any code which does the controller advice in detail..real time code or usage or code in any open source for reference to follow .....pattern in detail to follow...
0
girionisCommented:
I am afraid I don't have any other code to show you. I take it all projects that use ControllerAdvice use similar snippets of code.
0
Software ProgrammerAuthor Commented:
When Model and ModelMap needs to be used and what is the difference ? Once we know the difference we can close this.
0
girionisCommented:
Both are used to pass data to the view.  They are more or less the same. Model is an interface while ModelMap is an implementation of a Map.
0

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
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
Java EE

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.