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 !!


34 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. 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
  9. 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
  10. I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
    Blueprism training in btm

    Blueprism online training

    ReplyDelete
  11. 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
  12. 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
  13. 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
  14. 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
  15. indeed, I’m just always astounded concerning the remarkable things served by you. Some four facts on this page are undeniably the most effective I’ve had.
    SOFTWARE TRAINING IN CHENNAI
    POWERBI TRAINING IN CHENNAI
    CCNA TRAINING IN CHENNAI
    ANDROID TRAINING IN CHENNAI

    ReplyDelete
  16. Appreciating the persistence you put into your blog and detailed information you provide.

    Oracle dba training chennai | oracle dba training course chennai

    ReplyDelete
  17. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Big data Hadoop Related information please visit our website hadoop classes in pune page!

    ReplyDelete
  18. Really i appreciate the effort you made to share the knowledge. The topic here i found was really effective...

    Start your journey with SAP S4 HANA Simple Logistics Training in Bangalore and get hands-on Experience with 100% Placement assistance from experts Trainers @Softgen Infotech Located in BTM Layout Bangalore. Expert Trainers with 8+ Years of experience, Free Demo Classes Conducted.

    ReplyDelete
  19. Thanks for one marvelous posting! I enjoyed reading it; you are a great author. I will make sure to bookmark your blog and may come back someday. I want to encourage that you continue your great posts.Informatica Training in Bangalore

    ReplyDelete
  20. I am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing.

    Become an Expert In Python Training in Bangalore ! The most trusted and trending Programming Language. Learn from experienced Trainers and get the knowledge to crack a coding interview, @Softgen Infotech Located in BTM Layout.

    ReplyDelete
  21. Amazing and interested post..
    Thanks for sharing with us,
    We are again come on your website,
    Thanks and good day,
    Please visit our site,
    buylogo

    ReplyDelete
  22. Title:
    Best Software Training in Chennai | Infycle Technologies

    Description:
    Infycle Technologies is the best software training institute in Chennai and is widely known for its excellence in giving the best software training in Chennai. Providing quality software programming training with 100% assured placement & to build a strong career for every individual and young professionals in the IT industry is the ultimate aim of Infycle Technologies. Apart from all, the students love the 100% hands-on training, which is the specialty of Infycle Technologies. To proceed your career with a solid base, reach Infycle Technologies through 7502633633.

    Best training in Chennai

    ReplyDelete