Monday, December 22, 2014

Custom Exception Handling in ADF - Model Layer Generic Framework Design

I have observed that there are lot of users who asks questions on OTN and have problem in designing the custom generic exception handling framework. So in this and next some of the posts I will be describing how we can lay down a generic way to report custom exceptions to the page. These custom exceptions may be mostly for Business Specific errors for which we need to display a custom user friendly message on the page to the user.

In this post we will be seeing how we can customize the error on Model layer and take error messages from a resource bundle defined in the application.

Lets get started. We will first create a generic framework project where we will add exception handling related classes. We give it a name as FrameworkUtil. Framework util project will be dependent on model project from where custom Exceptions will be thrown in the enterprise applications.

First lets set up FrameworkUtil project - ADF Model Project 



Created an ADF Model Project with below package structure. Added a resource bundle ErrorBundle.properties which will be having all the Business Errors which can be thrown from the applications model layer.  These errors will be in key value pairs in Error properties file.




Added two custom error codes and messages. One without any parameter CSS_BAD_EMPNO and another one CSS_EMP_NOT_IN_DEPT which takes two parameters as name of the Employee and Department.

Now we need to design our Exception classes. At this time we need to understand that the root exception which gets thrown by the framework is JboException. Its a runtime exception. JboException throws the default error codes and associated messages defined in the framework. So we will be extending our Root Exception Class from JboException class and will make to read the error codes and messages from our Error Resource Bundle.

Lets create a class. We can name it for now as ProjectException. This extends from JboException and has below source code.



As above, it overloads three constructors. One is for the String code to be looked up in ErrorBundle, second one is for error code as well as array of parameters which will be given to placeholders in message corresponding to Error Code and the third one which is the second one plus Exception object.

The point here is to see that inside all the three overloaded constructors, there is a call to getMessage(errorCodeInRB). If we see the implementation of this method, this actually gets the ErrorBundle and gets the Error Message defined in the Error Bundle corresponding to the Error Code sent as the parameter to the getMessage() method.

Now this Error message will be sent as the parameter to JboException constructor when super is called from ProjectException constructor.

Ok So we are done for this part to read error message from RB for the given code.

Lets create a class named ProjectBusinessException. This class extends the ProjectException. This is the exception which will be thrown from the business services layer.



Thats it. Now we need to test this by throwing the ProjectBusinessException from our Business Components layer.

So lets create a Model and ViewController project for ADF Application. Named them CommonModel and ConsumerViewController projects respectively.

We will add FrameworkUtil project as dependent to CommonModel project so that the exception classes which we created - ProjectException and ProjectBusinessException are available to this project.


So now we need to test by throwing ProjectBusinessException. Lets create a method in AmImpl class and expose it as service interface so that it can accessed by the page(ViewControllerProject).

Created a method testProjectException() in AmImpl class which does nothing but just throws ProjectBusinessException with Error code and optional parameters.



As this method is exposed as client interface, it is available in Data Controls. Just for testing purpose, dragged the method and created a button out of it on a jspx page testException.jspx in ViewController project.


Thats it. Lets run the page and see what happens on clicking the button (calling testProjectException) method of AmImpl class.




So our method testProjectException threw the ProjectBusinessException with ErrorCode CSS_EMP_NOT_IN_DEPT and parameters  new String[]{"Rohan","HR"} . ProjectException reads this error code from ErrorBundle and sets the message with parameters and is returned to the view.

Lets change the error code and see.


Lets again run the page and see the error.


So thats it. This design can be implemented to have exceptions to be thrown as Custom error which will be read from Resource Bundle. This can be very useful for  giving meaningful business errors to the users.

Next post will talk about how to extend DCErrorrHandlerImpl to massage particular exceptions and how to skip certain exceptions so that they are not reported to the client.


Thanks !!


30 comments:

  1. Most Welcome Pandu. Glad you liked it !!

    ReplyDelete
  2. How to get Access to FacesContext in model Project..here

    ReplyDelete
  3. I will implement this in my code :) :)
    Thanks Rohan

    ReplyDelete
  4. Hey Rohan, can you please share the link to your next post on handling custom exceptions using DCErrorrHandlerImpl

    ReplyDelete
  5. Hi Rohan, plz share the link to your next post using DCErrorrHandlerImpl. Thanks.

    ReplyDelete
  6. Hey Rohan, your articles on ADF Framework has always been very helpful and have always increase my knowledge and interest towards ADF. As u mentioned in your this article u will share post abt how to extend DCErrorrHandlerImpl. Can u share u the link if u have alread6y posted it. Thanks :)

    ReplyDelete
  7. Rohan, this is a mess of a solution.
    You are accessing FacesContext in your Model layer.
    In North Korea they shoot people for such ideas

    ReplyDelete



  8. Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. dot net training in chennai velachery | dot net training institute in velachery

    ReplyDelete
  9. It is really a great work and the way in which u r sharing the knowledge is excellent.Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article.. dot net training and placement in chennai | best dot net training in chennai

    ReplyDelete
  10. If you want your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (even if they're dating somebody else now) you have to watch this video
    right away...

    (VIDEO) Get your ex back with TEXT messages?

    ReplyDelete
  11. nice post! I really like and appreciate your work, thank you for sharing such a useful information about human resource laws and oracle, keep updating the information, hear i prefer some more information about jobs for your career hr jobs in hyderabad .

    ReplyDelete
  12. CIITN is located in Prime location in Noida having best connectivity via all modes of public transport. CIITN offer both weekend and weekdays courses to facilitate Hadoop aspirants. Among all Hadoop Training Institute in Noida , CIITN's Big Data and Hadoop Certification course is designed to prepare you to match all required knowledge for real time job assignment in the Big Data world with top level companies. CIITN puts more focus in project based training and facilitated with Hadoop 2.7 with Cloud Lab—a cloud-based Hadoop environment lab setup for hands-on experience.

    CIITNOIDA is the good choice for Big Data Hadoop Training in NOIDA in the final year. I have also completed my summer training from here. It provides high quality Hadoop training with Live projects. The best thing about CIITNOIDA is its experienced trainers and updated course content. They even provide you placement guidance and have their own development cell. You can attend their free demo class and then decide.

    Hadoop Training in Noida
    Big Data Hadoop Training in Noida

    ReplyDelete
  13. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.


    rpa Training in Chennai

    rpa Training in bangalore

    rpa Training in pune

    blueprism Training in Chennai

    blueprism Training in bangalore

    blueprism Training in pune

    rpa online training

    ReplyDelete
  14. Some us know all relating to the compelling medium you present powerful steps on this blog and therefore strongly encourage contribution from other ones on this subject while our own child is truly discovering a great deal. Have fun with the remaining portion of the year.

    Data Science with Python training in chenni
    Data Science training in chennai
    Data science training in velachery
    Data science training in tambaram
    Data Science training in OMR
    Data Science training in anna nagar
    Data Science training in chennai
    Data science training in Bangalore

    ReplyDelete
  15. Thanks for such a great article here. I was searching for something like this for quite a long time and at last I’ve found it on your blog. It was definitely interesting for me to read  about their market situation nowadays.
    python training institute in chennai
    python training in velachery
    python training institute in chennai

    ReplyDelete
  16. This is most informative and also this post most user friendly and super navigation to all posts... Thank you so much for giving this information to me.. 
    DevOps online Training|DevOps Training in USA
    Devops Training in Chennai
    Devops Training in Bangalore

    ReplyDelete
  17. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  18. I am so proud of you and your efforts and work make me realize that anything can be done with patience and sincerity. Well I am here to say that your work has inspired me without a doubt.

    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    ReplyDelete
  19. I think things like this are really interesting. I absolutely love to find unique places like this. It really looks super creepy though!! rpa training and placement in chennai

    ReplyDelete
  20. I am really happy with your blog because your article is very unique and powerful for new reader.
    Click here:
    selenium training in chennai
    selenium training in bangalore
    selenium training in Pune
    selenium training in pune
    Selenium Online Training

    http://rxwen.blogspot.com/2012/10/communication-between-android-widget.html

    ReplyDelete
  21. Really great work. Your article was very helpful.Thanks for sharing valuable points.Keep sharing.Thank You
    rpa training in chennai | rpa training in velachery | trending technologies list 2018

    ReplyDelete
  22. Thanks for such a nice article on Blueprism.Amazing information of Blueprism you have . Keep sharing and updating this wonderful blog on Blueprism
    Thanks and regards,
    blue prism training in chennai
    blue prism training institute in chennai
    Blueprism certification in chennai

    ReplyDelete