private static void OnServiceError()

in src/AlibabaCloud.OSS.V2/Internal/ClientImpl.cs [521:585]


        private static void OnServiceError(ResponseMessage response)
        {
            var statusCode = response.StatusCode;

            if (statusCode / 100 == 2) return;

            string? message = null;
            string? code = null;
            string? ec = null;
            string? requestId = null;
            var errorFields = new Dictionary<string, string>();
            var content = response.Content != null ? new StreamReader(response.Content).ReadToEnd() : "";

            if (string.IsNullOrEmpty(content) && response.Headers.TryGetValue("x-oss-err", out var val))
                content = Encoding.UTF8.GetString(Convert.FromBase64String(val));

            try
            {
                var xmlDoc = new XmlDocument();
                xmlDoc.LoadXml(content);
                var rootNode = xmlDoc.SelectSingleNode("Error");

                if (rootNode != null)
                {
                    foreach (XmlNode node in rootNode.ChildNodes) errorFields[node.Name] = node.InnerText;

                    errorFields.TryGetValue("Message", out message);
                    errorFields.TryGetValue("Code", out code);
                    errorFields.TryGetValue("RequestId", out requestId);
                    errorFields.TryGetValue("EC", out ec);
                }
                else
                {
                    message =
                        $"Not found tag <Error>, part response body {content.Substring(0, Math.Min(256, content.Length))}";
                }
            }
            catch (Exception)
            {
                //Ignore
            }

            code ??= "BadErrorResponse";

            message ??=
                $"Failed to parse xml from response body, part response body {content.Substring(0, Math.Min(256, content.Length))}";

            requestId ??= response.Headers.TryGetValue("x-oss-request-id", out var value) ? value : "";

            ec ??= response.Headers.TryGetValue("x-oss-ec", out value) ? value : "";

            var requestTime = response.Headers.TryGetValue("Date", out value) ? value : "";

            var details = new Dictionary<string, string>() {
                { "Code", code },
                { "Message", message },
                { "RequestId", requestId },
                { "Ec", ec },
                { "RequestTarget", $"{response.Request!.Method} {response.Request.RequestUri}" },
                { "TimeStamp", requestTime },
                { "Snapshot", content }
            };

            throw new ServiceException(statusCode, details, errorFields, response.Headers);
        }