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.
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.
global override Auth.OAuthRefreshResult refresh(Map<string,string> authProviderConfiguration,String refreshToken){ HttpRequest req = new HttpRequest(); String accessToken = null; String error = null; try { // DEVELOPER TODO: Make a refresh token flow using refreshToken passed // in as an argument to get the new access token // accessToken = ... } catch (System.CalloutException e) { error = e.getMessage(); } catch(Exception e) { error = e.getMessage(); throw new Auth.AuthProviderPluginException('My custom error'); } return new Auth.OAuthRefreshResult(accessToken,refreshToken, error); }
This example uses Auth.VerificationException to trigger verification if a user attempts to create an account without a high assurance session.
trigger testTrigger on Account (before insert) { Map<String, String> sessionMap = auth.SessionManagement.getCurrentSession(); if(!sessionMap.get('SessionSecurityLevel').equals('HIGH_ASSURANCE')) { throw new Auth.VerificationException( Auth.VerificationPolicy.HIGH_ASSURANCE, 'Insert Account'); } }