9

I'm trying to crystallize out some 'best practises' for Visualforce Page exception handling.

I think, at a minimum, a page should look something like this:

<apex:page controller="PageController">
    <apex:pageMessages />
    <apex:pageBlock rendered="{!NOT(HasFatalMessages)}" />
</apex:page>
  • always present is apex:pageMessages
  • when a fundamental misconfiguration is detected, the UI is hidden to prevent data entry

Then the controller class might look something like this:

public with sharing class PageController {

    public Boolean getHasFatalMessages() {
        return ApexPages.hasMessages(ApexPages.Severity.FATAL);
    }

    public PageController() {
        try {
            //check org configuration required
            //for this page (for example, custom
            //settings, field set members, etc)
        } catch (SomeDomainLayerException e) {
            ApexPages.addMessages(e);
        }
    }
}

I can't for the life of me work out how ApexPages.addMessages(e) determines severity. It always seems to be ApexPages.Severity.ERROR no matter what. How can I change that?

Or should I throw an uncaught exception to blow up the whole page, indicating bigger problems?

Matt and Neil
  • 32,894
  • 7
  • 105
  • 186
  • could you share your "best practices" for Visualforce page exception handling ? – apn Oct 01 '14 at 16:45
  • @apn feel free to ask as a new question - it may warrant discussion (and therefore not be a good format for SFSE) but will definitely get the attention it deserves. – Matt and Neil Oct 02 '14 at 17:49

3 Answers3

7

Try catching errors as below :

catch(Exception e)
        {
            String error = e.getMessage();
            ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,error));
        }

Instead of just doing ApexPages.addMessages(e);

Using this in conjunction with Apexpages.hasMessages()

if(some_condition){
                        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.Info,'Info not an error'));
        }
        if(ApexPages.hasMessages(ApexPages.Severity.Info)){
        // do something
        }

To set severity dynamically I would use getTypeName() and create a map/ custom setting with exception key and severity value

http://www.salesforce.com/us/developer/docs/dbcom_apex230/Content/apex_classes_exception_methods.htm

Rao
  • 16,691
  • 13
  • 68
  • 108
  • Cool. I figured it's a little more wordy, but one really does have to specify the severity and throw domain-specific exceptions in the domain layer. Exceptions do not carry severity. – Matt and Neil Sep 30 '14 at 23:11
  • As far as I know exception class only carry the exception info and not the severity level, you can make a map/ custom setting that carry severity levels based on exception that might occur. – Rao Sep 30 '14 at 23:19
  • 1
    I have a comment in some code (that uses ApexPages.addMessages(e)) that says: Using this signature method appears to both avoid duplication for errors added to fields in triggers and get declarative errors such as DUPLICATE_VALUE reported albeit in rather basic form e.g. "duplicate value found: cve__Temp__c duplicates value on record with id: 0035000001epEMl". This was in a context where DmlExceptions could be thrown. So ApexPages.addMessages probably contains some useful processing logic that you will lose if you use other patterns. – Keith C Sep 30 '14 at 23:20
  • 1
    plus be aware of this delightful undocumented behavior - http://salesforce.stackexchange.com/questions/50180/apexpages-addmessage-behavior-inconsistent-by-version – cropredy Oct 01 '14 at 00:11
3

Here are some examples of how to add messages with different severities and how they are outputted:

Confirm/Success:

ApexPages.addMessage(
     new ApexPages.Message(ApexPages.Severity.Confirm,'Sample Success Message'));

enter image description here


Warning:

ApexPages.addMessage(
    new ApexPages.Message(ApexPages.Severity.Warning,'Sample Warning Message'));

enter image description here


Informational:

ApexPages.addMessage(
    new ApexPages.Message(ApexPages.Severity.Info,'Sample Informational Message'));

enter image description here


Error:

ApexPages.addMessage(
    new ApexPages.Message(ApexPages.Severity.Error,'Sample Error Message'));

enter image description here


There's a great answer in this question that has lots more detail: Difference between the multiple messaging options in Visualforce?

BarCotter
  • 12,331
  • 4
  • 37
  • 58
-2

Severity for error message is defined when we add message in to page using our code. See code below for different severity:

if(acc.name == '' || acc.name == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.FATAL,'Please enter Account name'));

      if(acc.AccountNumber == '' || acc.AccountNumber == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.ERROR,'Please enter Account number'));

      if(acc.phone == '' || acc.phone == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.WARNING,'Please enter Account phone'));

      if(acc.site == '' || acc.site == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.INFO,'Please enter Account site'));

      if(acc.industry == '' || acc.industry == null)
       ApexPages.addmessage(new ApexPages.message(ApexPages.severity.CONFIRM,'Please enter Account industry'));

Error Message with different severity

For more details see below link:

http://www.sfdcpoint.com/salesforce/show-error-message-visualforce-page/

Ankush Dureja
  • 1,029
  • 7
  • 19