diff --git a/Mailosaur.Test/ErrorsTests.cs b/Mailosaur.Test/ErrorsTests.cs new file mode 100644 index 0000000..d97ae76 --- /dev/null +++ b/Mailosaur.Test/ErrorsTests.cs @@ -0,0 +1,46 @@ +using System; +using Mailosaur.Models; +using Xunit; + +namespace Mailosaur.Test +{ + public class ErrorsTests + { + [Fact] + public void UnauthorizedTest() + { + var client = new MailosaurClient("invalid_key"); + var ex = Assert.Throws(delegate + { + client.Servers.List(); + }); + + Assert.Equal("Authentication failed, check your API key.", ex.Message); + } + + [Fact] + public void NotFoundTest() + { + var client = new MailosaurClient(Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY")); + var ex = Assert.Throws(delegate + { + client.Servers.Get("not_found"); + }); + + Assert.Equal("Not found, check input parameters.", ex.Message); + } + + [Fact] + public void BadRequestTest() + { + var client = new MailosaurClient(Environment.GetEnvironmentVariable("MAILOSAUR_API_KEY")); + var options = new ServerCreateOptions(""); + var ex = Assert.Throws(delegate + { + client.Servers.Create(options); + }); + + Assert.Equal("(name) Please provide a name for your server\r\n", ex.Message); + } + } +} diff --git a/Mailosaur.Test/ServersTests.cs b/Mailosaur.Test/ServersTests.cs index fad5272..7c507e6 100644 --- a/Mailosaur.Test/ServersTests.cs +++ b/Mailosaur.Test/ServersTests.cs @@ -87,7 +87,7 @@ public void FailedCreateTest() m_Client.Servers.Create(options); }); - Assert.Equal("Request had one or more invalid parameters.", ex.Message); + Assert.Equal("(name) Please provide a name for your server\r\n", ex.Message); Assert.Equal("invalid_request", ex.ErrorType); Assert.Equal(400, ex.HttpStatusCode); Assert.Contains("{\"type\":", ex.HttpResponseBody); diff --git a/Mailosaur/Models/ErrorResponse.cs b/Mailosaur/Models/ErrorResponse.cs new file mode 100644 index 0000000..00840d6 --- /dev/null +++ b/Mailosaur/Models/ErrorResponse.cs @@ -0,0 +1,20 @@ +namespace Mailosaur.Models +{ + using System.Collections.Generic; + + public class ErrorResponse + { + public IList Errors { get; set; } + } + + public class Error + { + public string Field { get; set; } + public IList Detail { get; set; } + } + + public class ErrorDetail + { + public string Description { get; set; } + } +} diff --git a/Mailosaur/Operations/OperationBase.cs b/Mailosaur/Operations/OperationBase.cs index dd2b944..4794e34 100644 --- a/Mailosaur/Operations/OperationBase.cs +++ b/Mailosaur/Operations/OperationBase.cs @@ -55,10 +55,11 @@ public async Task ExecuteRequest(HttpMethod method, string path, object bo if (typeof(T) != typeof(MessageListResultWithHeaders)) return JsonConvert.DeserializeObject(content); - + response.Headers.TryGetValues("x-ms-delay", out IEnumerable delayHeaderValues); - - return (T)(object)new MessageListResultWithHeaders() { + + return (T)(object)new MessageListResultWithHeaders() + { MessageListResult = JsonConvert.DeserializeObject(content), DelayHeader = delayHeaderValues?.FirstOrDefault() }; @@ -124,10 +125,11 @@ public T HandleAggregateException(Func requestMethod) public string PagePath(string path, int? page = null, int? itemsPerPage = null, DateTime? receivedAfter = null) { string isoReceivedAfter = null; - if (receivedAfter != null) { + if (receivedAfter != null) + { isoReceivedAfter = WebUtility.UrlEncode(receivedAfter.Value.ToString("o")); } - + path += page != null ? $"&page={page}" : ""; path += itemsPerPage != null ? $"&itemsPerPage={itemsPerPage}" : ""; path += receivedAfter != null ? $"&receivedAfter={isoReceivedAfter}" : ""; @@ -139,9 +141,18 @@ private async Task ThrowExceptionAsync(HttpResponseMessage response) string errorMessage = ""; string errorType = ""; - switch (response.StatusCode) { + var httpResponseBody = (response.StatusCode != HttpStatusCode.NoContent) ? + await response.Content.ReadAsStringAsync() : ""; + + switch (response.StatusCode) + { case HttpStatusCode.BadRequest: - errorMessage = "Request had one or more invalid parameters."; + var json = JsonConvert.DeserializeObject(httpResponseBody); + foreach (var err in json.Errors) + { + errorMessage += $"({err.Field}) {err.Detail[0].Description}\r\n"; + } + // errorMessage = "Request had one or more invalid parameters."; errorType = "invalid_request"; break; case HttpStatusCode.Unauthorized: @@ -153,7 +164,7 @@ private async Task ThrowExceptionAsync(HttpResponseMessage response) errorType = "permission_error"; break; case HttpStatusCode.NotFound: - errorMessage = "Request did not find any matching resources."; + errorMessage = "Not found, check input parameters."; errorType = "invalid_request"; break; default: @@ -162,10 +173,7 @@ private async Task ThrowExceptionAsync(HttpResponseMessage response) break; } - var httpResponseBody = (response.StatusCode != HttpStatusCode.NoContent) ? - await response.Content.ReadAsStringAsync() : ""; - throw new MailosaurException(errorMessage, errorType, (int)response.StatusCode, httpResponseBody); } } -} \ No newline at end of file +}