From 43aa297ac22d314699e0a75df93f132dde911e5f Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Tue, 22 Sep 2020 19:42:50 -0700 Subject: [PATCH] Improving the logging of WorkspaceClientException to include more actionable information (#380) Improving the logging of WorkspaceClientException to include more informative data in its message from its inner exception. This is a follow up to #378 . - Instead of providing the full stack of the exception in the output, we extract more relevant fields from the source RestErrorException and provide them to the user. - Also, this change exposes header "x-ms-request-id"if present in the response, for additional debugging. --- .../Exceptions/WorkspaceClientException.cs | 35 ++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs b/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs index 821e3a64bc2..ee1b7293182 100644 --- a/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs +++ b/src/Azure/Azure.Quantum.Client/Exceptions/WorkspaceClientException.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. +using Microsoft.Azure.Quantum.Client.Models; using System; namespace Microsoft.Azure.Quantum.Exceptions @@ -67,9 +68,41 @@ public WorkspaceClientException( $"WorkspaceName: {workspaceName}{Environment.NewLine}" + $"BaseUri: {baseUri}{Environment.NewLine}" + $"JobId: {jobId}{Environment.NewLine}" + - (inner != null ? $"Inner Exception: {inner}" : string.Empty), + FormatInnerException(inner), inner) { } + + /// + /// Formats the contents of the inner exception in so it can be included in the + /// exception message and presented in an informative way. + /// + /// Inner exception that we want to include in the outer exception message. + /// + /// A string representing the contents of the inner exception. + /// + private static string FormatInnerException(Exception ex) + { + string formattedException = string.Empty; + if (ex != null) + { + formattedException += $"Server Error: {ex.Message}{Environment.NewLine}"; + + // Handle specific types of exceptions for additional data + if (ex is RestErrorException restErrorException) + { + formattedException += $"Error Code: {restErrorException?.Body?.Code}{Environment.NewLine}" + + $"Server message: {restErrorException?.Body?.Message}{Environment.NewLine}"; + + var headers = restErrorException?.Response?.Headers; + if (headers != null && headers.ContainsKey("x-ms-request-id")) + { + formattedException += $"Server Request Id: {headers["x-ms-request-id"]}{Environment.NewLine}"; + } + } + } + + return formattedException; + } } }