diff options
author | Alex Reed <alexreed@outlook.com> | 2014-09-01 03:16:56 +0100 |
---|---|---|
committer | Alex Reed <alexreed@outlook.com> | 2014-09-01 03:16:56 +0100 |
commit | b6766f3d56fd72ce1ce22864efc9c267abe398c6 (patch) | |
tree | bafa70271c8cdb45df61049e5741580a7088b06f /SendGrid/SendGridMail/Transport/Web.cs | |
parent | 4671685b65127de3db6739f3d9a9d92174f3b327 (diff) | |
download | sendgrid-csharp-b6766f3d56fd72ce1ce22864efc9c267abe398c6.zip sendgrid-csharp-b6766f3d56fd72ce1ce22864efc9c267abe398c6.tar.gz sendgrid-csharp-b6766f3d56fd72ce1ce22864efc9c267abe398c6.tar.bz2 |
Added new exception catching.
Diffstat (limited to 'SendGrid/SendGridMail/Transport/Web.cs')
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 52 |
1 files changed, 31 insertions, 21 deletions
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index f241256..750cb48 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -7,6 +7,7 @@ using System.Net.Http; using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Xml;
+using Exceptions;
using SendGrid.SmtpApi;
// ReSharper disable MemberCanBePrivate.Global
@@ -56,19 +57,19 @@ namespace SendGrid /// Asynchronously delivers a message over SendGrid's Web interface
/// </summary>
/// <param name="message"></param>
- public async Task DeliverAsync(ISendGrid message)
- {
- var client = new HttpClient
- {
- BaseAddress = new Uri("https://" + BaseUrl)
- };
-
- var content = new MultipartFormDataContent();
- AttachFormParams(message, content);
- AttachFiles(message, content);
- var response = await client.PostAsync(Endpoint + ".xml", content);
- await CheckForErrorsAsync(response);
- }
+ public async Task DeliverAsync(ISendGrid message)
+ {
+ var client = new HttpClient
+ {
+ BaseAddress = new Uri("https://" + BaseUrl)
+ };
+
+ var content = new MultipartFormDataContent();
+ AttachFormParams(message, content);
+ AttachFiles(message, content);
+ var response = await client.PostAsync(Endpoint + ".xml", content);
+ await CheckForErrorsAsync(response);
+ }
#region Support Methods
@@ -118,15 +119,16 @@ namespace SendGrid private static void CheckForErrors(HttpResponseMessage response)
{
- //transport error
- if (response.StatusCode != HttpStatusCode.OK)
- {
- throw new Exception(response.ReasonPhrase);
- }
-
var content = response.Content.ReadAsStreamAsync().Result;
+ var errors = GetErrorsInResponse(content);
- FindErrorsInResponse(content);
+ // API error
+ if (errors.Any())
+ throw new InvalidApiRequestException(response.StatusCode, errors, response.ReasonPhrase);
+
+ // Other error
+ if (response.StatusCode != HttpStatusCode.OK)
+ FindErrorsInResponse(content);
}
private static void FindErrorsInResponse(Stream content)
@@ -153,6 +155,13 @@ namespace SendGrid }
}
+ private static string[] GetErrorsInResponse(Stream content)
+ {
+ var xmlDoc = new XmlDocument();
+ xmlDoc.Load(content);
+ return (from XmlNode errorNode in xmlDoc.SelectNodes("//error") select errorNode.InnerText).ToArray();
+ }
+
private static async Task CheckForErrorsAsync(HttpResponseMessage response)
{
//transport error
@@ -189,7 +198,8 @@ namespace SendGrid .Concat(message.To.ToList().Select(a => new KeyValuePair<String, String>("toname[]", a.DisplayName)))
.ToList();
}
- if (message.GetEmbeddedImages().Count > 0) {
+ if (message.GetEmbeddedImages().Count > 0)
+ {
result = result.Concat(message.GetEmbeddedImages().ToList().Select(x => new KeyValuePair<String, String>(string.Format("content[{0}]", x.Key), x.Value)))
.ToList();
}
|