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
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); }
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.
@ControllerAdvicepublic class ExceptionConfiguration { @ResponseStatus(HttpStatus.CONFLICT) @ExceptionHandler(RuntimeException.class) public void handleRuntimeException() {} @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(CustomException.class) public void handleCustomException() {}}
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.
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...
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
Open in new window
But of course all this depends on the use case.