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 | |
parent | 4671685b65127de3db6739f3d9a9d92174f3b327 (diff) | |
download | sendgrid-csharp-b6766f3d56fd72ce1ce22864efc9c267abe398c6.zip sendgrid-csharp-b6766f3d56fd72ce1ce22864efc9c267abe398c6.tar.gz sendgrid-csharp-b6766f3d56fd72ce1ce22864efc9c267abe398c6.tar.bz2 |
Added new exception catching.
-rw-r--r-- | SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs | 19 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Mail.csproj | 8 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 52 | ||||
-rw-r--r-- | SendGrid/SendGridMail/packages.config | 2 |
4 files changed, 57 insertions, 24 deletions
diff --git a/SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs b/SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs new file mode 100644 index 0000000..7fd51ea --- /dev/null +++ b/SendGrid/SendGridMail/Exceptions/InvalidApiRequestException.cs @@ -0,0 +1,19 @@ +using System; +using System.Net; + +namespace Exceptions +{ + public class InvalidApiRequestException : Exception + { + public InvalidApiRequestException(HttpStatusCode httpStatusCode, string[] errors, string httpResponsePhrase) + : base(httpResponsePhrase + " Check `Errors` for a list of errors returned by the API.") + { + ResponseStatusCode = httpStatusCode; + Errors = errors; + } + + public String[] Errors { get; set; } + + public HttpStatusCode ResponseStatusCode { get; private set; } + } +} diff --git a/SendGrid/SendGridMail/Mail.csproj b/SendGrid/SendGridMail/Mail.csproj index 19ec745..9dcb4ad 100644 --- a/SendGrid/SendGridMail/Mail.csproj +++ b/SendGrid/SendGridMail/Mail.csproj @@ -98,6 +98,7 @@ <Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
+ <Compile Include="Exceptions\InvalidApiRequestException.cs" />
<Compile Include="ISendGrid.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="SendGrid.cs" />
@@ -105,7 +106,9 @@ <Compile Include="Transport\Web.cs" />
</ItemGroup>
<ItemGroup>
- <None Include="packages.config" />
+ <None Include="packages.config">
+ <SubType>Designer</SubType>
+ </None>
<None Include="sendgrid-csharp.snk" />
</ItemGroup>
<ItemGroup>
@@ -130,6 +133,7 @@ <Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
+ <ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
@@ -139,4 +143,4 @@ <Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
\ No newline at end of file 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();
}
diff --git a/SendGrid/SendGridMail/packages.config b/SendGrid/SendGridMail/packages.config index d05ec44..eff5517 100644 --- a/SendGrid/SendGridMail/packages.config +++ b/SendGrid/SendGridMail/packages.config @@ -1,4 +1,4 @@ <?xml version="1.0" encoding="utf-8"?>
<packages>
- <package id="SendGrid.SmtpApi" version="1.1.3" targetFramework="net40" />
+ <package id="SendGrid.SmtpApi" version="1.1.3" targetFramework="net45" />
</packages>
\ No newline at end of file |