Skip to content

Commit

Permalink
Add clearer error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
jm-mailosaur committed Jun 14, 2022
1 parent f490325 commit 2a37d40
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 13 deletions.
46 changes: 46 additions & 0 deletions Mailosaur.Test/ErrorsTests.cs
Original file line number Diff line number Diff line change
@@ -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<MailosaurException>(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<MailosaurException>(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<MailosaurException>(delegate
{
client.Servers.Create(options);
});

Assert.Equal("(name) Please provide a name for your server\r\n", ex.Message);
}
}
}
2 changes: 1 addition & 1 deletion Mailosaur.Test/ServersTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 20 additions & 0 deletions Mailosaur/Models/ErrorResponse.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace Mailosaur.Models
{
using System.Collections.Generic;

public class ErrorResponse
{
public IList<Error> Errors { get; set; }
}

public class Error
{
public string Field { get; set; }
public IList<ErrorDetail> Detail { get; set; }
}

public class ErrorDetail
{
public string Description { get; set; }
}
}
32 changes: 20 additions & 12 deletions Mailosaur/Operations/OperationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ public async Task<T> ExecuteRequest<T>(HttpMethod method, string path, object bo

if (typeof(T) != typeof(MessageListResultWithHeaders))
return JsonConvert.DeserializeObject<T>(content);

response.Headers.TryGetValues("x-ms-delay", out IEnumerable<string> delayHeaderValues);

return (T)(object)new MessageListResultWithHeaders() {

return (T)(object)new MessageListResultWithHeaders()
{
MessageListResult = JsonConvert.DeserializeObject<MessageListResult>(content),
DelayHeader = delayHeaderValues?.FirstOrDefault()
};
Expand Down Expand Up @@ -124,10 +125,11 @@ public T HandleAggregateException<T>(Func<T> 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}" : "";
Expand All @@ -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<ErrorResponse>(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:
Expand All @@ -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:
Expand All @@ -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);
}
}
}
}

0 comments on commit 2a37d40

Please sign in to comment.