sendgrid-csharp: Response contains HTTP code 200 and error in body if client sends an email when disconnected from Internet
Issue Summary
When I try to send an email without network connection to remote SMTP server, a response contains 200 OK in StatusCode and HttpRequestException in Body. I think it denies basic HTTP principles and such behaviour is completely unclear. I propose to throw a real exception. If impossible, the code from 4xx-5xx range should be used.
Steps to Reproduce
var sg = new SendGridAPIClient("my_api_key");
Mail mail = new Mail(from, subject, to, content);
//disconnect from Internet before executing this line
var response = await sg.client.mail.send.post(requestBody: mail.Get());
//writes "OK"
Console.WriteLine(response.StatusCode);
//writes ".NET HttpRequestException, raw message: An error occurred while sending the request"
Console.WriteLine(response.Body.ReadAsStringAsync().Result);
Technical details:
- sendgrid-csharp Version: v8.0.5
- .NET Version: 4.5.2
About this issue
- Original URL
- State: closed
- Created 10 years ago
- Reactions: 4
- Comments: 25 (15 by maintainers)
Commits related to this issue
- Merge pull request #434 from ottomatic/issue_358 Issue #358: SendGridClient.SendEmailAsync now throws original exception — committed to sendgrid/sendgrid-csharp by thinkingserious 10 years ago
- Version Bump v9.1.1: #358: SendGridClient.SendEmailAsync now throws original exception — committed to sendgrid/sendgrid-csharp by thinkingserious 10 years ago
OK, so the pull request has been submitted: https://github.com/sendgrid/sendgrid-csharp/pull/434
This is my first PR ever! I think the process so far has been rather painless. Good guidance in the contributing.md document regarding which branch to base the PR on, ow to rebase, etc.
In line with making this library more unit test friendly as @ottomatic has suggested. Would there be any interest in exposing an ISendGridClient etc that the existing classes implement?
I for one would find it beneficial to be able to easily mock up a SendGridClient for my own unit tests, but cannot because none of the methods are virtual etc.
My preferred mocking library is Moq, but as things stand I have had to go down the road of using Microsofts Fakes to make a ShimSendGridClient just so I can write tests for specific response codes being returned.
@thinkingserious ,
Yes, still intending to do this. I have been busy consuming the SMTP Api instead, since our production servers don’t allow outgoing http(s) through the firewall - whereas SMTP on port 587 is allowed.
One thing I would like to do in order to make unit testing easier is to take an HttpClient as an injected dependency in the SendGridAPIClient constructor. This would make it possible to inject a custom RequestHandler into the HttpClient, and in that way take control over the http response. What do you think of this change?
Awesome, my pleasure.
@benmiller86 Your suggestion is already formalised in issue #403 and earlier in #201 . I think that would be a good idea and will upvote.
Allright. I have cloned the repo, and have successfully gotten started on the development.
Yes, the PR which @Jericho refers to is pretty much what I want to accomplish, and then use that to write a unit test in order to set up the throwing of an exception (e.g. a TimeoutException) when performing the httpclient call. I assume the PR was rejected because it was made against the wrong branch and did not comwe with a signed CLA?
So, the easiest path forward for me is do do both the HttpClient injection and the exception handling (i.e. NOT swallowing the exception) in the same PR. Otherwise I will have to wait for one PR to be approved in order to be able to continue with the issue at hand.
@SendGridDX , Is this approach acceptable?
We’ve been having huge problems with this too. It’s completely misleading. It shouldn’t even be an HTTP response because it didn’t even get that far. It’s just a piece of c# code calling another piece of .net code and should get an appropriate exception. If it does have to be an HTTP response then @chumakov-ilya is correct it should be an error code.
I could look into submitting a pull request for this if the library maintainers are interested.
Hi @chumakov-ilya,
Thanks! We agree and I’ve added this to our backlog for a fix.