Skip to content

Commit

Permalink
Merge pull request #365 from SharebookBR/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
raffacabofrio authored Jun 16, 2020
2 parents cb842d7 + 5a4d02e commit d8e3fd8
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 d8e3fd8

Please sign in to comment.