MSAL4j - How to handle MsalThrottlingException?

977 Views Asked by At

I use MSAL4j and there is an exception type named MsalThrottlingException. How can I handle it when I catch it? I need an example implementation.

try{
    Future<IAuthenticationResult> future = 
    confidentialClientApplication.acquireToken(authorizationCodeParameters);
    IAuthenticationResult authenticationResult = future.get(1, TimeUnit.MINUTES); 
}
catch(ExecutionException e){
    if(e.getCause() instanceof MsalThrottlingException){
        //how to handle it
    }
}

enter image description here https://learn.microsoft.com/en-us/azure/active-directory/develop/msal-error-handling-java

There was a document about it also(you can see the screen shot in above link), but it doesn't give a example handling implementation. Could you give an example?

1

There are 1 best solutions below

0
On

This worked for me:

    private static String PREFIX_RETRY_STR = "com.microsoft.aad.msal4j.MsalThrottlingException: Request was throttled according to instructions from STS. Retry in ";
    private static String SUFFIX_RETRY_STR = " ms.";

(...)

            if (e.getCause() instanceof MsalThrottlingException) {
                int waitTime = Integer.parseInt(e.getMessage().replace(PREFIX_RETRY_STR, "").replace(SUFFIX_RETRY_STR, ""));
                try {
                    TimeUnit.MILLISECONDS.sleep(waitTime);
                } catch (InterruptedException interruptedException) {
                    interruptedException.printStackTrace();
                }
                result = pca.acquireToken(parameters).join();
            } else
                throw e;