Avatar of Software Programmer
Software Programmer
 asked on

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

Avatar of undefined
Last Comment
girionis

8/22/2022 - Mon
girionis

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.
Software Programmer

ASKER
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 ???
girionis

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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Software Programmer

ASKER
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.
girionis

Software Programmer

ASKER
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...
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
girionis

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.
Software Programmer

ASKER
When Model and ModelMap needs to be used and what is the difference ? Once we know the difference we can close this.
ASKER CERTIFIED SOLUTION
girionis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.