Repository: Azure/azure-sdk-for-net
File: sdk/identity/Azure.Identity/src/Credentials/AzureDeveloperCliCredential.cs (C#)

Code before:
            using JsonDocument document = JsonDocument.Parse(output);

            JsonElement root = document.RootElement;
            string accessToken = root.GetProperty("token").GetString();
            DateTimeOffset expiresOn = root.GetProperty("expiresOn").GetDateTimeOffset();
            return new AccessToken(accessToken, expiresOn);
        }

        private static string ProcessCliErrorForDisplay(string rawCliError)
        {
            // Strategy: Try to parse azd JSON format {"type":"...","data":{"message":"..."}}, extract message, fallback to raw
            string displayText = rawCliError;

            // Guard: skip processing if null/empty or doesn't start with brace
            string trimmedError = rawCliError?.TrimStart();
            bool skipParsing = string.IsNullOrEmpty(trimmedError) || trimmedError[0] != '{';
            if (skipParsing)
                return displayText;

            JsonDocument jsonDoc = null;
            try
            {
                jsonDoc = JsonDocument.Parse(rawCliError);

                if (jsonDoc != null)
                {
                    JsonElement root = jsonDoc.RootElement;
                    JsonElement dataObj;
                    bool foundData = root.TryGetProperty("data", out dataObj);

                    if (foundData)
                    {
                        JsonElement msgObj;
                        bool foundMsg = dataObj.TryGetProperty("message", out msgObj);

                        if (foundMsg)
                        {
                            string msgText = msgObj.GetString();
                            bool hasContent = !string.IsNullOrWhiteSpace(msgText);

                            if (hasContent)
                            {
                                displayText = msgText.Trim();
                            }
                        }
                    }
                }
            }
            catch
            {
                // Parsing failed, keep original output

Review comment (suggestion):
@copilot  Let's reduce nesting and change this logic such that if jsonDoc == null, we return `rawCliuError` immediately.

Code after:
            {
                fileName = "/bin/sh";
                argument = $"-c \"{command}\"";
            }
        }

        private static AccessToken DeserializeOutput(string output)
        {
            using JsonDocument document = JsonDocument.Parse(output);

            JsonElement root = document.RootElement;
            string accessToken = root.GetProperty("token").GetString();
            DateTimeOffset expiresOn = root.GetProperty("expiresOn").GetDateTimeOffset();
            return new AccessToken(accessToken, expiresOn);
        }

        private static string ProcessCliErrorForDisplay(string rawCliError)
        {
            // Strategy: Try to parse azd JSON format {"type":"...","data":{"message":"..."}}, extract message, fallback to raw
            // Guard: skip processing if null/empty or doesn't start with brace
            string trimmedError = rawCliError?.TrimStart();
            bool skipParsing = string.IsNullOrEmpty(trimmedError) || trimmedError[0] != '{';
            if (skipParsing)
                return rawCliError;

            try
            {
                using JsonDocument jsonDoc = JsonDocument.Parse(rawCliError);
                JsonElement root = jsonDoc.RootElement;

                if (root.TryGetProperty("data", out JsonElement dataObj) &&
                    dataObj.TryGetProperty("message", out JsonElement msgObj))
                {
                    string msgText = msgObj.GetString();
                    if (!string.IsNullOrWhiteSpace(msgText))
                    {
                        return msgText.Trim();
                    }
                }
            }
            catch (JsonException)
            {
                // Parsing failed, keep original output
            }

            return rawCliError;
        }
    }
}