This week some of our code (that was running without issue for the last month) started misbehaving. Our code was using SharePoint CSOM and PnP Core calls inside an Azure Function, and intermittently we were catching exceptions saying Connection to SPO DS failed. with our local stacktrace.

Due to the nature of the issues, I wanted Microsoft involved and was redirected to the SP-Dev-Docs repository to discus this in the open. I was asked to provide the correlation id for the requests that were failing, and was provided a very helpful article to get it for requests you do with the browser in SharePoint (and by extension, when doing REST API calls).
For requests done through CSOM, you can probably use Fiddler to capture the requests, but wouldn't it be a lot easier if we could get this from the actual exception and properly log this?

Luckily, all that information is provided through the ServerException class provided with the SharePoint CSOM package. We adjusted our code to catch this specific exception, and log all relevant additional properties that it provides:

                //code that creates the exception
            catch (ServerException e)
                log.LogError($"Message: {e.Message}");
                log.LogError($"ServerErrorCode: {e.ServerErrorCode}");
                log.LogError($"ServerErrorDetails: {e.ServerErrorDetails}");
                log.LogError($"ServerErrorTraceCorrelationId: {e.ServerErrorTraceCorrelationId}");
                log.LogError($"ServerErrorTypeName: {e.ServerErrorTypeName}");
                log.LogError($"ServerErrorValue: {e.ServerErrorValue}");
                log.LogError($"ServerStackTrace: {e.ServerStackTrace}");
                log.LogError($"Source: {e.Source}");
                log.LogError($"StackTrace: {e.StackTrace}");


Of course, in the mean time the error stopped occurring 🙄 At least, we now have the correct logging in place if/when it starts showing again 😀.