This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor(MasaException): Refactor MasaException (#82)
* Refactor(MasaException): Refactor MasaException * refactor(Exception): Support MasaExceptionHandler * chore: format code * chore: format code
- Loading branch information
1 parent
fe83cf2
commit 43f11be
Showing
34 changed files
with
713 additions
and
93 deletions.
There are no files selected for viewing
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
9 changes: 9 additions & 0 deletions
9
src/Caller/Masa.Utils.Caller.Core/DefaultRequestIdGenerator.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,9 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Utils.Caller.Core; | ||
|
||
public class DefaultRequestIdGenerator : IRequestIdGenerator | ||
{ | ||
public string NewId() => Guid.NewGuid().ToString(); | ||
} |
32 changes: 32 additions & 0 deletions
32
src/Caller/Masa.Utils.Caller.Core/DefaultRequestMessage.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,32 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Utils.Caller.Core; | ||
|
||
public abstract class DefaultRequestMessage | ||
{ | ||
private readonly string _requestIdKey; | ||
private readonly IRequestIdGenerator _requestIdGenerator; | ||
private readonly IHttpContextAccessor? _httpContextAccessor; | ||
|
||
public DefaultRequestMessage(string requestIdKey, IRequestIdGenerator requestIdGenerator, | ||
IHttpContextAccessor? httpContextAccessor = null) | ||
{ | ||
_requestIdKey = requestIdKey; | ||
_requestIdGenerator = requestIdGenerator; | ||
_httpContextAccessor = httpContextAccessor; | ||
} | ||
|
||
protected void TrySetRequestId(HttpRequestMessage requestMessage) | ||
{ | ||
var httpContext = _httpContextAccessor?.HttpContext; | ||
if (httpContext == null) | ||
return; | ||
|
||
if (!httpContext.Request.Headers.TryGetValue(_requestIdKey, out var requestId)) | ||
requestId = _requestIdGenerator.NewId(); | ||
|
||
if (requestMessage.Headers.All(h => h.Key != _requestIdKey)) | ||
requestMessage.Headers.Add(_requestIdKey, requestId.ToString()); | ||
} | ||
} |
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,9 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Utils.Caller.Core; | ||
|
||
public interface IRequestIdGenerator | ||
{ | ||
string NewId(); | ||
} |
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
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
80 changes: 80 additions & 0 deletions
80
src/Masa.Utils.Exceptions/Handlers/ExceptionHandlerMiddleware.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,80 @@ | ||
// Copyright (c) MASA Stack All rights reserved. | ||
// Licensed under the MIT License. See LICENSE.txt in the project root for license information. | ||
|
||
namespace Masa.Utils.Exceptions.Handlers; | ||
|
||
public class ExceptionHandlerMiddleware | ||
{ | ||
private readonly RequestDelegate _next; | ||
private readonly IMasaExceptionHandler? _masaExceptionHandler; | ||
private readonly MasaExceptionHandlerOptions _options; | ||
private readonly MasaExceptionLogRelationOptions _logRelationOptions; | ||
private readonly ILogger<ExceptionHandlerMiddleware>? _logger; | ||
|
||
public ExceptionHandlerMiddleware( | ||
RequestDelegate next, | ||
IServiceProvider serviceProvider, | ||
IOptions<MasaExceptionHandlerOptions> options, | ||
IOptions<MasaExceptionLogRelationOptions> logRelationOptions, | ||
ILogger<ExceptionHandlerMiddleware>? logger = null) | ||
{ | ||
_next = next; | ||
_options = options.Value; | ||
_masaExceptionHandler = ExceptionHandlerExtensions.GetMasaExceptionHandler(serviceProvider, _options.MasaExceptionHandlerType); | ||
_logRelationOptions = logRelationOptions.Value; | ||
_logger = logger; | ||
} | ||
|
||
public async Task InvokeAsync(HttpContext httpContext) | ||
{ | ||
try | ||
{ | ||
await _next(httpContext); | ||
} | ||
catch (Exception exception) | ||
{ | ||
var masaExceptionContext = new MasaExceptionContext(exception, httpContext); | ||
if (_options.ExceptionHandler != null) | ||
{ | ||
_options.ExceptionHandler.Invoke(masaExceptionContext); | ||
} | ||
else if (_masaExceptionHandler != null) | ||
{ | ||
_masaExceptionHandler.OnException(masaExceptionContext); | ||
} | ||
|
||
if (httpContext.Response.HasStarted) | ||
return; | ||
|
||
if (masaExceptionContext.ExceptionHandled) | ||
{ | ||
await httpContext.Response.WriteTextAsync( | ||
masaExceptionContext.StatusCode, | ||
masaExceptionContext.Message ?? masaExceptionContext.Exception.Message, | ||
masaExceptionContext.ContentType); | ||
return; | ||
} | ||
|
||
_logger?.WriteLog(masaExceptionContext.Exception, | ||
masaExceptionContext.Exception is UserFriendlyException ? LogLevel.Information : LogLevel.Error, | ||
_logRelationOptions); | ||
|
||
if (masaExceptionContext.Exception is UserFriendlyException) | ||
{ | ||
await httpContext.Response.WriteTextAsync((int)MasaHttpStatusCode.UserFriendlyException, | ||
masaExceptionContext.Exception.Message); | ||
} | ||
else if (masaExceptionContext.Exception is MasaException || _options.CatchAllException) | ||
{ | ||
var message = Constant.DEFAULT_EXCEPTION_MESSAGE; | ||
await httpContext.Response.WriteTextAsync((int)HttpStatusCode.InternalServerError, message); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
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.