Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Communications.HttpPostAsync() swallows httpClient exceptions without logging #60

Open
vbodo opened this issue Feb 14, 2023 · 1 comment

Comments

@vbodo
Copy link

vbodo commented Feb 14, 2023

The _client.PostAsync() call is wrapped in a try/catch block. If the HttpClient throws an exception, the exception is caught and the method returns null without logging anything. This makes it very difficult to identify and troubleshoot communication issues such as timeouts.

        /// <summary>
        /// Sends a POST request with the given XML to the API, asynchronously
        /// Prefer the use of this method over HttpPost
        /// </summary>
        /// <param name="xmlRequest">The XML to send to the API</param>
        /// <param name="cancellationToken"></param>
        /// <returns>The XML response on success, null otherwise</returns>
        public async Task<string> HttpPostAsync(string xmlRequest, CancellationToken cancellationToken)
        {
            // First, read values from the config that we need that relate to logging
            _config.TryGetValue("logFile", out var logFile);
            var printXml = _config.ContainsKey("printxml") && "true".Equals(_config["printxml"]);

            // Log any data to the appropriate places, only if we need to
            if (printXml)
            {
                Console.WriteLine(xmlRequest);
                Console.WriteLine(logFile);
            }
            if (logFile != null)
            {
                Log(xmlRequest, logFile);
            }
            
            // Now that we have gotten the values for logging from the config, we need to actually send the request
            try
            {
                OnHttpAction(RequestType.Request, xmlRequest);
                var xmlContent = new StringContent(xmlRequest, Encoding.UTF8, "application/xml");
                var response = await _client.PostAsync(_config["url"], xmlContent, cancellationToken);
                var xmlResponse = await response.Content.ReadAsStringAsync();
                OnHttpAction(RequestType.Response, xmlResponse);

                if (printXml)
                {
                    Console.WriteLine(xmlResponse);
                }
                if (logFile != null)
                {
                    Log(xmlResponse, logFile);
                }

                return xmlResponse;
            }
            catch (Exception)
            {
                return null;
            }
        }
@isunnapud
Copy link
Contributor

We will update the proper logging associated with error response in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants