Tuesday, December 20, 2016

Visualforce - Beyond Basics Series 32 - How to handle error properly

What could go wrong with Visualforce Code ?

When the visualforce page renders in UI, it goes through different layers of processing,

  1.   Controller/Extension Object Creation for page
  2.   Invoke Action method for Page
  3.   Controller Creation for Component
  4.   Initialization of Components
  5.   Rendering each binding variables
In above steps, the error could happen in any step. It could be due to Invalid Data (Or) Improper handling of invalid data (Or) Developer induced error to User.


What are the places to take more care ?

One need to take more care in following cases,
  Handling null values - To avoid, initialize all variables in Constructor or at declaration.
  Handling mathematical exceptions - To avoid, careful about divide by zero cases
  Handling Network exception - To avoid, always try-catch CalloutException
  Handling DML exceptions - To avoid, always try-catch DMLException
  Handling NoDataFound exception - To avoid, always try catch Soql query section
There are many other possible cases, but i have highlighted common cases above.


What are the ways to report error ?

Between controller & visualforce page, there's one way of error communication which is through ApexPages.Message

How to handle them gracefully ?

Ideally, there should be no unhandled exception. In that case, salesforce will throw the raw error to User without proceeding after the error.

Where can the error be reported in UI ?

It can be reported in following places in UI,

  1.   Top section in the page
  2.   Above any group of components
  3.   Exactly below the component


Where is the Code ?

I have illustrated on few ways of reporting error in Controller & also ways to display them in visualforce page


Any Screenshot ?



Can you walk through in video ?


No comments:

Post a Comment

Thanks for reading my blog !