Create Custom Error Messages in the Login Discovery Handler

Create Custom Error Messages in the Login Discovery Handler

If a user makes a mistake when trying to log in to your site, the Apex handler displays a generic error message. You can customize the error message that appears on the login page using the Auth.DiscoveryCustomErrorException exception.

Required Editions

Available in: Salesforce Classic and Lightning Experience
Available in: Enterprise, Performance, Unlimited, and Developer Editions
User Permissions Needed
To access Experience Workspaces:
  • Access Experience Management OR Manage Experiences OR Create and Set Up Experiences
  • AND is a member of the Experience Cloud site
To create and edit Visualforce pages: Customize Application
To edit Apex classes:
  • Author Apex
  • AND Customize Application
  1. From Setup, in the Quick Find box, enter Apex, and then select Apex Classes.
  2. From the list of Apex classes, find the class that implements Auth.LoginDiscoveryHandler.
    You can find the name of the handlers on the Login & Registration page of the Administration workspace. If you haven’t renamed the handler, the name is similar to AutocreatedDiscLoginHandler1535677786093.
  3. To add the custom error message, edit the Apex class using the Auth.DiscoveryCustomErrorException exception.
    For example, throw new Auth.DiscoveryCustomErrorException('Custom error message defined by the admin.').
  4. (Optional) Instead of entering the custom error message in the Apex class, you can create the error message as a custom label.
    Custom labels are custom text values that can be translated to the user’s native language. In this example, CustomError is a custom label.

    throw new Auth.DiscoveryCustomErrorException(Label.CustomError)

    Note
    Note You create a custom error message from the Custom Labels Setup page. The error message can be up to 200 characters.

Login Discovery custom error messages appear on the login page.