-
Notifications
You must be signed in to change notification settings - Fork 507
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(#217) [Asp.Net Core 8] IExceptionHandler
- Loading branch information
1 parent
128b6fe
commit d800f1e
Showing
17 changed files
with
445 additions
and
326 deletions.
There are no files selected for viewing
106 changes: 106 additions & 0 deletions
106
...vices/Common/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
using ClassifiedAds.CrossCuttingConcerns.Exceptions; | ||
using ClassifiedAds.Infrastructure.Logging; | ||
using Microsoft.AspNetCore.Diagnostics; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Logging; | ||
using Microsoft.Extensions.Options; | ||
using System; | ||
using System.Diagnostics; | ||
using System.Net; | ||
using System.Text.Json; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace ClassifiedAds.Infrastructure.Web.ExceptionHandlers; | ||
|
||
public class GlobalExceptionHandler : IExceptionHandler | ||
{ | ||
private readonly ILogger<GlobalExceptionHandler> _logger; | ||
private readonly GlobalExceptionHandlerOptions _options; | ||
|
||
public GlobalExceptionHandler(ILogger<GlobalExceptionHandler> logger, IOptions<GlobalExceptionHandlerOptions> options) | ||
{ | ||
_logger = logger; | ||
_options = options.Value; | ||
} | ||
|
||
public async ValueTask<bool> TryHandleAsync(HttpContext httpContext, Exception exception, CancellationToken cancellationToken) | ||
{ | ||
var response = httpContext.Response; | ||
|
||
if (exception is NotFoundException) | ||
{ | ||
var problemDetails = new ProblemDetails | ||
{ | ||
Detail = exception.Message, | ||
Instance = null, | ||
Status = (int)HttpStatusCode.NotFound, | ||
Title = "Not Found", | ||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.4" | ||
}; | ||
|
||
problemDetails.Extensions.Add("message", exception.Message); | ||
problemDetails.Extensions.Add("traceId", Activity.Current.GetTraceId()); | ||
|
||
response.ContentType = "application/problem+json"; | ||
response.StatusCode = problemDetails.Status.Value; | ||
|
||
var result = JsonSerializer.Serialize(problemDetails); | ||
await response.WriteAsync(result, cancellationToken: cancellationToken); | ||
|
||
return true; | ||
} | ||
else if (exception is ValidationException) | ||
{ | ||
var problemDetails = new ProblemDetails | ||
{ | ||
Detail = exception.Message, | ||
Instance = null, | ||
Status = (int)HttpStatusCode.BadRequest, | ||
Title = "Bad Request", | ||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.5.1" | ||
}; | ||
|
||
problemDetails.Extensions.Add("message", exception.Message); | ||
problemDetails.Extensions.Add("traceId", Activity.Current.GetTraceId()); | ||
|
||
response.ContentType = "application/problem+json"; | ||
response.StatusCode = problemDetails.Status.Value; | ||
|
||
var result = JsonSerializer.Serialize(problemDetails); | ||
await response.WriteAsync(result, cancellationToken: cancellationToken); | ||
|
||
return true; | ||
} | ||
else | ||
{ | ||
_logger.LogError(exception, "[{Ticks}-{ThreadId}]", DateTime.UtcNow.Ticks, Environment.CurrentManagedThreadId); | ||
|
||
if (_options.DetailLevel == GlobalExceptionDetailLevel.Throw) | ||
{ | ||
return false; | ||
} | ||
|
||
var problemDetails = new ProblemDetails | ||
{ | ||
Detail = _options.GetErrorMessage(exception), | ||
Instance = null, | ||
Status = (int)HttpStatusCode.InternalServerError, | ||
Title = "Internal Server Error", | ||
Type = "https://datatracker.ietf.org/doc/html/rfc7231#section-6.6.1" | ||
}; | ||
|
||
problemDetails.Extensions.Add("message", _options.GetErrorMessage(exception)); | ||
problemDetails.Extensions.Add("traceId", Activity.Current.GetTraceId()); | ||
|
||
response.ContentType = "application/problem+json"; | ||
response.StatusCode = problemDetails.Status.Value; | ||
|
||
var result = JsonSerializer.Serialize(problemDetails); | ||
await response.WriteAsync(result, cancellationToken: cancellationToken); | ||
|
||
return true; | ||
} | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
...ommon/ClassifiedAds.Infrastructure/Web/ExceptionHandlers/GlobalExceptionHandlerOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
|
||
namespace ClassifiedAds.Infrastructure.Web.ExceptionHandlers; | ||
|
||
public class GlobalExceptionHandlerOptions | ||
{ | ||
public GlobalExceptionDetailLevel DetailLevel { get; set; } | ||
|
||
public string GetErrorMessage(Exception ex) | ||
{ | ||
return DetailLevel switch | ||
{ | ||
GlobalExceptionDetailLevel.None => "An internal exception has occurred.", | ||
GlobalExceptionDetailLevel.Message => ex.Message, | ||
GlobalExceptionDetailLevel.StackTrace => ex.StackTrace, | ||
GlobalExceptionDetailLevel.ToString => ex.ToString(), | ||
_ => "An internal exception has occurred.", | ||
}; | ||
} | ||
} | ||
|
||
public enum GlobalExceptionDetailLevel | ||
{ | ||
None, | ||
Message, | ||
StackTrace, | ||
ToString, | ||
Throw, | ||
} |
103 changes: 0 additions & 103 deletions
103
src/Microservices/Common/ClassifiedAds.Infrastructure/Web/Filters/GlobalExceptionFilter.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.