diff options
author | Brandon West <brawest@gmail.com> | 2014-10-09 15:52:10 -0600 |
---|---|---|
committer | Brandon West <brawest@gmail.com> | 2014-10-09 15:52:10 -0600 |
commit | 88bbde7219098f8699dfa971e6c88a879e01c3fb (patch) | |
tree | 38ad6fd56c28b405f65eae1f59dd29a1573b2b70 | |
parent | fc46788ff6da5e31559e0550607db735ecde4c2d (diff) | |
download | sendgrid-csharp-88bbde7219098f8699dfa971e6c88a879e01c3fb.zip sendgrid-csharp-88bbde7219098f8699dfa971e6c88a879e01c3fb.tar.gz sendgrid-csharp-88bbde7219098f8699dfa971e6c88a879e01c3fb.tar.bz2 |
async exception handling example
-rw-r--r-- | SendGrid/Example/Program.cs | 31 | ||||
-rw-r--r-- | SendGrid/SendGridMail/Transport/Web.cs | 10 |
2 files changed, 30 insertions, 11 deletions
diff --git a/SendGrid/Example/Program.cs b/SendGrid/Example/Program.cs index 9da69f5..fa97707 100644 --- a/SendGrid/Example/Program.cs +++ b/SendGrid/Example/Program.cs @@ -17,18 +17,29 @@ namespace Example myMessage.Subject = "Testing the SendGrid Library";
myMessage.Text = "Hello World!";
- // Create credentials, specifying your user name and password.
- var credentials = new NetworkCredential("username", "password");
+ SendAsync(myMessage);
- // Create a Web transport for sending email.
- var transportWeb = new Web(credentials);
-
- // Send the email.
- if (transportWeb != null)
- transportWeb.DeliverAsync(myMessage);
-
- Console.WriteLine("Done!");
Console.ReadLine();
}
+
+ private static async void SendAsync(SendGridMessage message)
+ {
+ // Create credentials, specifying your user name and password.
+ var credentials = new NetworkCredential("username", "password");
+
+ // Create a Web transport for sending email.
+ var transportWeb = new Web(credentials);
+
+ // Send the email.
+ try
+ {
+ await transportWeb.DeliverAsync(message);
+ Console.WriteLine("Sent!");
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine(ex.Message);
+ }
+ }
}
}
\ No newline at end of file diff --git a/SendGrid/SendGridMail/Transport/Web.cs b/SendGrid/SendGridMail/Transport/Web.cs index 1458da8..69fed4c 100644 --- a/SendGrid/SendGridMail/Transport/Web.cs +++ b/SendGrid/SendGridMail/Transport/Web.cs @@ -179,7 +179,15 @@ namespace SendGrid var content = await response.Content.ReadAsStreamAsync();
- FindErrorsInResponse(content);
+ 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)
|