• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Best Practice Of the Following 4 Approaches

 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Error Handling can be done with any of the following approach

Example1: Controller throws exception whichever service layer throws it

https://github.com/aasthasmile/Spring-Boot-MVC-WebBasedApplication/blob/master/src/main/java/csulb/edu/aasthajain/controller/UserController.java

Example2: Catching exception in controller and throwing Status Object at last with error message

https://github.com/SurendraVidiyala/SpringRestCrud/blob/master/src/main/java/com/javacrud/controller/RestController.java

Example3: Creating exception in Controller and throwing the exception directly

https://github.com/khoubyari/spring-boot-rest-example/blob/master/src/main/java/com/khoubyari/example/api/rest/HotelController.java

Example4: Open Source project shopizer approach. catch exception in controller layer and sets the response status

https://github.com/shopizer-ecommerce/shopizer/blob/2.0.5/sm-shop/src/main/java/com/salesmanager/shop/controller/ReferenceController.java

Question:

Which one is the best approach or standard and efficient practice?
Is there is any other good approach then the above, if yes, please let know in detail.

Thanks.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch!

I would immediately reject the first two examples, because you shouldn't return success statuses (200) for erroneous responses. A simple "success" or "failure" is the worst because it doesn't tell you what went wrong, only that something went wrong. The second one does something similar in some cases, returning null or a 0 status.

Personally I'd go for the third example. Just let the method throw the exception, and use a controller advice to catch this and map it to a proper response (or you can use the default if that's good enough for you). The fourth one just seems so cluttered. You have to do all of the exception handling in every single method.

One note about the third example: if your class is annotated with @RestController you get @ResponseBody for free, there is no need to keep repeating it. With @Controller you do need to add it where necessary.
 
Kathir jeyap
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot Rob Spoor.

We need to handle runtime as well as business exception (ParseException, Sales Order Not Found, Payment Error, Unable to connect to third party server)

Can you please help me with a code snippet of controller advice which handles both custom and business exception?

I'm confused what code should exist in controlleradvice which is being left in throwing exceptions and why we need it ???



 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Creating a controller advice is quite simple:

You can add catch-all methods with @ExceptionHandler(Exception.class) or even @ExceptionHandler(Throwable.class), but then you have to ensure that each exception gets mapped correctly.
 
Kathir jeyap
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But my question is how to append error message properties ?? We can have generic error handling..However how will it resolve business errors

ex:

Unable to retrieve Sales Order "123" since purchase order id "455" doesn't exist for User "Raj"

Like this the customized exception will have error message and the values to it.

We need to define it to handle for all. Please advise
 
Rob Spoor
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The exact error output depends on your business needs, and how the front-end code can handle it. We tend to return a JSON object (with status 400, 500, etc) that includes some code and message, and possibly other useful information.
 
Kathir jeyap
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

So can we have a new status code for WARNING, ERROR, INFO and message, so that UI handles accordingly.

Cause in web application we cannot differentiate the type in the front end until the back-end talks about it.


Is that correct, please advise.
 
Kathir jeyap
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 if you don't understand what i expect.
 
reply
    Bookmark Topic Watch Topic
  • New Topic