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.
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.
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.
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 !!
Thanks. Very helpful
ReplyDeleteMost Welcome Pandu. Glad you liked it !!
ReplyDeleteExcellent Rohan,keep going
ReplyDeleteThanks Srinivas !!
ReplyDeleteHow to get Access to FacesContext in model Project..here
ReplyDeleteI will implement this in my code :) :)
ReplyDeleteThanks Rohan
Hey Rohan, can you please share the link to your next post on handling custom exceptions using DCErrorrHandlerImpl
ReplyDeleteHi Rohan, plz share the link to your next post using DCErrorrHandlerImpl. Thanks.
ReplyDeleteHey 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 :)
ReplyDeleteRohan, this is a mess of a solution.
ReplyDeleteYou are accessing FacesContext in your Model layer.
In North Korea they shoot people for such ideas
you made my day :D
DeleteThanks for sharing with us.
ReplyDeleteJava Training in Chennai
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteautomation anywhere training in chennai
automation anywhere training in bangalore
automation anywhere training in pune
automation anywhere online training
blueprism online training
rpa Training in sholinganallur
rpa Training in annanagar
iot-training-in-chennai
blueprism-training-in-pune
automation-anywhere-training-in-pune
I really appreciate this post. I’ve been looking all over for this! Thank goodness I found it on Bing. You’ve made my day! Thx again!
ReplyDeletejava training in marathahalli | java training in btm layout
java training in jayanagar | java training in electronic city
java training in chennai | java training in USA
selenium training in chennai
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.
ReplyDeletepython training institute in chennai
python training in velachery
python training institute in chennai
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..
ReplyDeleteDevOps online Training|DevOps Training in USA
Devops Training in Chennai
Devops Training in Bangalore
I believe there are many more pleasurable opportunities ahead for individuals that looked at your site.
ReplyDeleteBlueprism training in btm
Blueprism online training
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.
ReplyDeleteangularjs Training in bangalore
angularjs Training in btm
angularjs Training in electronic-city
angularjs online Training
angularjs Training in marathahalli
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
ReplyDeleteI am really happy with your blog because your article is very unique and powerful for new reader.
ReplyDeleteClick 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
Really great work. Your article was very helpful.Thanks for sharing valuable points.Keep sharing.Thank You
ReplyDeleterpa training in chennai | rpa training in velachery | trending technologies list 2018
Thanks for your sharing
ReplyDeleteMicrosoft Azure Training in Chennai
Openstack Training in Chennai
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.
ReplyDeleteSOFTWARE TRAINING IN CHENNAI
POWERBI TRAINING IN CHENNAI
CCNA TRAINING IN CHENNAI
ANDROID TRAINING IN CHENNAI
I enjoy what you guys are usually up too. This sort of clever work and coverage! Keep up the wonderful works guys I’ve added you guys to my blog roll.
ReplyDeleteAWS Course Training in Chennai |Best AWS Training Institute in Chennai
Devops Course Training in Chennai |Best Devops Training Institute in Chennai
Selenium Course Training in Chennai |Best Selenium Training Institute in Chennai
Java Course Training in Chennai | Best Java Training Institute in Chennai
Appreciating the persistence you put into your blog and detailed information you provide.
ReplyDeleteOracle dba training chennai | oracle dba training course chennai
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..
ReplyDeleteIf you are looking for any Big data Hadoop Related information please visit our website hadoop classes in pune page!
i think this is the best post ethical hacking online training india
ReplyDeleteReally i appreciate the effort you made to share the knowledge. The topic here i found was really effective...
ReplyDeleteStart 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.
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
ReplyDeleteI am happy for sharing on this blog its awesome blog I really impressed. thanks for sharing.
ReplyDeleteBecome 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.
Excellent idea and it is the best blog for this topic...
ReplyDeleteLinux Training in Chennai
Linux Course in Chennai
Advanced Excel Training in Chennai
Oracle Training in Chennai
Graphic Design Courses in Chennai
Unix Training in Chennai
Tableau Training in Chennai
Oracle DBA Training in Chennai
Power BI Training in Chennai
Pega Training in Chennai
Spark Training in Chennai
Amazing and interested post..
ReplyDeleteThanks for sharing with us,
We are again come on your website,
Thanks and good day,
Please visit our site,
buylogo
I’ve desired to post about something similar to this on one of my blogs and this has given me an idea. Cool Mat.thanks alot keep update
ReplyDeleteAi & Artificial Intelligence Course in Chennai
PHP Training in Chennai
Ethical Hacking Course in Chennai Blue Prism Training in Chennai
UiPath Training in Chennai
Title:
ReplyDeleteBest 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