diff options
author | Howard van Rooijen <Howard.vanRooijen@endjin.com> | 2015-02-04 21:49:29 +0000 |
---|---|---|
committer | Howard van Rooijen <Howard.vanRooijen@endjin.com> | 2015-02-06 07:23:55 +0000 |
commit | 2705013d2b58d0e614205606c87c4ad2ecec8259 (patch) | |
tree | 93e610e85a2bf722bc0ce2581a42e386e84ccf2a /SendGrid/SendGridMail/Transport/Web.cs | |
parent | 6354302c92e8994b44ae8fc89a992ad4ffca5b6d (diff) | |
download | sendgrid-csharp-2705013d2b58d0e614205606c87c4ad2ecec8259.zip sendgrid-csharp-2705013d2b58d0e614205606c87c4ad2ecec8259.tar.gz sendgrid-csharp-2705013d2b58d0e614205606c87c4ad2ecec8259.tar.bz2 |
Refactor error handling. Add test for checking for bad username / password scenario
Diffstat (limited to 'SendGrid/SendGridMail/Transport/Web.cs')
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 66 |
1 files changed, 4 insertions, 62 deletions
diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index 7abf104..a125833 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -66,7 +66,7 @@ namespace SendGrid AttachFormParams(message, content);
AttachFiles(message, content);
var response = client.PostAsync(Endpoint + ".xml", content).Result;
- CheckForErrors(response);
+ ErrorChecker.CheckForErrors(response);
}
/// <summary>
@@ -87,7 +87,7 @@ namespace SendGrid AttachFormParams(message, content);
AttachFiles(message, content);
var response = await client.PostAsync(Endpoint + ".xml", content);
- await CheckForErrorsAsync(response);
+ await ErrorChecker.CheckForErrorsAsync(response);
}
#region Support Methods
@@ -136,66 +136,8 @@ namespace SendGrid }
}
- private static void CheckForErrors(HttpResponseMessage response)
- {
- var content = response.Content.ReadAsStreamAsync().Result;
- var errors = GetErrorsInResponse(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)
- {
- using (var reader = XmlReader.Create(content))
- {
- while (reader.Read())
- {
- if (!reader.IsStartElement()) continue;
- switch (reader.Name)
- {
- case "result":
- break;
- case "message": // success
- if (reader.ReadToNextSibling("errors"))
- throw new ProtocolViolationException();
- return;
- case "error": // failure
- throw new ProtocolViolationException();
- default:
- throw new ArgumentException("Unknown element: " + reader.Name);
- }
- }
- }
- }
-
- 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)
- {
- var content = await response.Content.ReadAsStreamAsync();
-
- var errors = GetErrorsInResponse(content);
-
- // API error
- if (errors.Any())
- throw new InvalidApiRequestException(response.StatusCode, errors, response.ReasonPhrase);
-
- // Other error
- if (response.StatusCode != HttpStatusCode.OK)
- FindErrorsInResponse(content);
- }
-
+
+
internal List<KeyValuePair<String, String>> FetchFormParams(ISendGrid message)
{
var result = new List<KeyValuePair<string, string>>
|