title: Format stack trace to manually return in API response
body: <p>Currently it is returning a single unintelligible chunk composed of multiple of these things glued one next to the other like this:</p>
  <blockquote>
  <p>at ProyectX.Services.Service.Validate(IList 1 myParam) in
  C:\\Repositories\\projectx\\src\\ProyectX.Services\\Service.cs:line
  116\r\n at ProyectX.Services.Service.Validate(IList 1 myParam) in
  C:\\Repositories\\projectx\\src\\ProyectX.Services\\Service.cs:line
  116\r\n</p>
  </blockquote>
  <p>Goal:</p>
  <blockquote>
  <p>at ProyectX.Services.Service.Validate(IList 1 myParam) in C:\Repositories\projectx\src\ProyectX.Services\Service.cs:line 116</p>
  <p>at ProyectX.Services.Service.Validate(IList 1 myParam) in C:\Repositories\projectx\src\ProyectX.Services\Service.cs:line 116</p>
  </blockquote>
  <p>I tried with</p>
  <pre><code>Regex.Unescape(exception.StackTrace)
  
  JsonSerializer.Serialize(exception.StackTrace, new JsonSerializerOptions() {WriteIndented = true });
  </code></pre>
  <p>The middleware is in Startup.cs</p>
  <pre><code>public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
  {
      app.UseMiddleware&lt;ErrorHandlerMiddleware&gt;();
  </code></pre>
  <p>Middleweare:</p>
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Net;
  using System.Text.Json;
  using System.Threading.Tasks;
  using Microsoft.AspNetCore.Hosting;
  using Microsoft.AspNetCore.Http;
  using Microsoft.Extensions.Hosting;
  using ProyectX.Domain.Exceptions;
  using ProyectX.Domain.Models;
  
  namespace ProyectX.API.Middleware
  {
      public class ErrorHandlerMiddleware
      {
          private readonly RequestDelegate _next;
  
          public ErrorHandlerMiddleware(RequestDelegate next)
          {
              _next = next;
          }
  
          public async Task Invoke(HttpContext context, IWebHostEnvironment env)
          {
              try
              {
                  await _next(context);
              }
              catch (Exception exception)
              {
                  var response = context.Response;
                  response.ContentType = &quot;application/json&quot;;
  
                  switch (exception)
                  {
                      case InvalidOperationException:
                          response.StatusCode = (int)HttpStatusCode.BadRequest;
                          break;
                      default:
                          response.StatusCode = (int)HttpStatusCode.InternalServerError;
                          break;
                  }
  
                  var details = new Dictionary&lt;string, string&gt;
                  {
                      { &quot;Message&quot;, exception.Message },
                  };
  
                  if (env.IsDevelopment())
                  {
                      details.Add(&quot;StackTrace&quot;, exception.StackTrace);
                  }
  
                  var errorResponse = new ErrorResponseModel(exception, details);
                  var result = JsonSerializer.Serialize(errorResponse);
  
                  await response.WriteAsync(result);
              }
          }
      }
  }
  </code></pre>
  <p>ErrorResponseModel</p>
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace ProyectX.Domain.Models
  {
      public class ErrorResponseModel
      {
          public ErrorResponseModel(Exception ex, Dictionary&lt;string, string&gt; details)
          {
              Type = ex.GetType().Name;
              Details = details;
          }
  
          public string Type { get; set; }
  
          public IDictionary&lt;string, string&gt; Details { get; set; }
      }
  }
  </code></pre>