Newer Version Available

This content describes an older version of this product. View Latest

Auth Exceptions

The Auth namespace contains some exception classes.

All exception classes support built-in methods for returning the error message and exception type. See Exception Class and Built-In Exceptions.

The Auth namespace contains the following exception.

Exception Description Methods
Auth.​AuthProviderPluginException Throw this exception to indicate that an error occurred when using the auth provider plug-in. Use to display a custom error message to the user. To get the error message and write it to debug log, use the String getMessage().
Auth.​ConnectedAppPlugin​Exception Throw this exception to indicate that an error occurred while running the custom behavior for a connected app. To get the error message and write it to debug log, use the String getMessage().
Auth.DiscoveryCustomErrorException Throw this exception to customize error messages that appear on Login Discovery or Configurable Self-Registration pages. The error message can have up to 200 characters.

Throw this exception in a class that implements Auth.LoginDiscoveryHandler to show the error message on the login page. Throw this exception in a class that implements Auth.ConfigurableSelfRegHandler to show the error message on the verify page, if you selected the Email or Text Message verification method. If you selected None, the error message appears on the self-registration page.

To get the error message and write it to debug log, use the String getMessage().
Auth.JWTBearerTokenExchange.​JWTBearerTokenExchangeException​ Throw this exception to indicate a problem with the response from the token endpoint in the JWTBearerTokenExchange class. This exception occurs when the HTTP response during the OAuth 2.0 JWT bearer token flow:
  • Fails to return an access token.
  • Is not in JSON format.
  • Returns a response code other than a 200 “OK” success code.
To get the error message and write it to debug log, use the String getMessage().
Auth.LoginDiscoveryException Throw this exception to indicate that an error occurred when executing the Login Discovery handler. For an example, see LoginDiscoveryHandler Example Implementation. To get the error message and write it to debug log, use the String getMessage().
Auth.VerificationException Throw this exception to trigger verification based on the passed-in policy. You can throw this exception in an Apex trigger or Visualforce controller. The system automatically sends you to the verification endpoint, if possible.

After you throw this exception, you cannot catch it. The exception immediately triggers the verification.

Note

Not applicable

Examples

This example uses AuthProviderPluginException to throw a custom error message on any method in a custom authentication provider implementation. Use this exception if you want the end user to see a specific message, passing in the error message as a parameter. If you use another exception, users see a standard Salesforce error message.

1global override Auth.OAuthRefreshResult refresh(Map<string,string> authProviderConfiguration,String refreshToken){
2            HttpRequest req = new HttpRequest();
3            String accessToken = null;
4            String error  = null;
5            try {
6            
7            // DEVELOPER TODO: Make a refresh token flow using refreshToken passed 
8            // in as an argument to get the new access token
9            // accessToken = ... 
10            } catch (System.CalloutException e) {
11            error = e.getMessage();
12            }
13            catch(Exception e) {
14            error = e.getMessage();
15            throw new Auth.AuthProviderPluginException('My custom error');
16            }
17            
18            return new Auth.OAuthRefreshResult(accessToken,refreshToken, error);                
19            }

This example uses Auth.VerificationException to trigger verification if a user attempts to create an account without a high assurance session.

1trigger testTrigger on Account (before insert) {
2    Map<String, String> sessionMap = auth.SessionManagement.getCurrentSession();
3    if(!sessionMap.get('SessionSecurityLevel').equals('HIGH_ASSURANCE')) {
4        throw new Auth.VerificationException(
5            Auth.VerificationPolicy.HIGH_ASSURANCE, 'Insert Account');
6    }
7}