Skip to content

Commit

Permalink
Resolve o problema de camelCase nas exceções.
Browse files Browse the repository at this point in the history
  • Loading branch information
raffacabofrio committed Jun 15, 2020
1 parent 2cf08ac commit fd4ba34
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ShareBook/ShareBook.Api/Middleware/ExceptionHandlerMiddleware.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Newtonsoft.Json;
using Newtonsoft.Json.Serialization;
using Rollbar;
using ShareBook.Api.Services;
using ShareBook.Domain.Common;
Expand All @@ -29,7 +30,7 @@ public async Task Invoke(HttpContext httpContext)
{
var result = new Result();
result.Messages.Add(ex.Message);
var jsonResponse = JsonConvert.SerializeObject(result);
var jsonResponse = ToJson(result);

httpContext.Response.Clear();
httpContext.Response.StatusCode = (int)ex.ErrorType;
Expand All @@ -50,7 +51,7 @@ public async Task Invoke(HttpContext httpContext)
if (ex is AggregateException)
result.Messages.Add(ex.InnerException.ToString());

var jsonResponse = JsonConvert.SerializeObject(result);
var jsonResponse = ToJson(result);

httpContext.Response.Clear();
httpContext.Response.StatusCode = StatusCodes.Status500InternalServerError;
Expand All @@ -59,6 +60,22 @@ public async Task Invoke(HttpContext httpContext)
}
}

private string ToJson(Object obj)
{
DefaultContractResolver contractResolver = new DefaultContractResolver
{
NamingStrategy = new CamelCaseNamingStrategy()
};

string json = JsonConvert.SerializeObject(obj, new JsonSerializerSettings
{
ContractResolver = contractResolver,
Formatting = Formatting.Indented
});

return json;
}

private void SendErrorToRollbar(Exception ex)
{
object error = new
Expand Down

0 comments on commit fd4ba34

Please sign in to comment.