title: Is this good to handle errors globally with custom error interceptor in .net core Web Api
body: <p>I want to ask you guys is this good to handle errors globally or better way is to handle them with try catch blocks.
  Here are 2 different ways you can handle errors:</p>
  <p><strong>Global handler</strong></p>
  <pre><code>public class ErrorDetails : Exception
  {
      public int StatusCode { get; }
      public override string Message { get; }
      public override string ToString()
      {
          return JsonConvert.SerializeObject(new {Message});
      }
  
      public ErrorDetails(int statusCode, string message) 
      {
          Message = message;
          StatusCode = statusCode;
      }
  }
  public class GlobalErrorHandler
  {
      private readonly RequestDelegate _next;
      public GlobalErrorHandler(RequestDelegate next)
      {
          _next = next;
      }
      public async Task InvokeAsync(HttpContext httpContext)
      {
          try
          {
              await _next(httpContext);
          }
          catch (ErrorDetails ex)
          {
              await HandleExceptionAsync(httpContext, ex);
          }
          catch (Exception ex)
          {
              await HandleExceptionAsync(httpContext, ex);
          }
      }
      private Task HandleExceptionAsync(HttpContext context, ErrorDetails exception)
      {
          context.Response.ContentType = &quot;application/json&quot;;
          context.Response.StatusCode = exception.StatusCode;
          return context.Response.WriteAsync(exception.ToString());
      }
      private Task HandleExceptionAsync(HttpContext context, Exception exception)
      {
          context.Response.ContentType = &quot;application/json&quot;;
          context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
          return context.Response.WriteAsync(exception.ToString());
      }
  }
  </code></pre>
  <p>And then just throw errors in code or there is also second approach</p>
  <pre><code>public async Task&lt;Response&lt;User&gt;&gt; Create(User user)
      {
          try
          {
              var userFromDatabase = await _repository.Queryable().FirstOrDefaultAsync(x =&gt; x.Email == user.Email);
              if (userFromDatabase != null) return new Response&lt;User&gt;(&quot;Taki użytkownik już istnieje.&quot;);
              user.Password = new PasswordHasher&lt;string?&gt;().HashPassword(null, user.Password);
              await _repository.InsertAsync(user);
              return new Response&lt;User&gt;(user);
          }
          catch(Exception ex)
          {
              return new Response&lt;User&gt;($&quot;Error: {ex.Message}&quot;);
          }
          
      }
  public class Response&lt;TEntity&gt; where TEntity : class
  {
      public readonly string Message;
      public readonly bool Success;
      public readonly TEntity Entity;
      public readonly List&lt;TEntity&gt; Entities;
      private Response(TEntity entity, List&lt;TEntity&gt; entities, string message, bool success)
      {
          Entity = entity;
          Entities = entities;
          Success = success;
          Message = message;
      }
      public Response(string message) : this(null, null, message, false) {}
      public Response(TEntity entity) : this(entity, null, null, true) {}
      public Response(List&lt;TEntity&gt; entities) : this(null, entities, null,true) {}
  }
  </code></pre>
  <p>This approach is based on Response obj that is being sent to controller and then in controller based on <code>Response&lt;TEntity&gt;.Success</code> value we sent to user object or message.</p>
  <p>My question is: is global error handling ok or it is better to use try catch in every service.</p>
  <p>Thanks in advance :)</p>