ปรับ Middleware โดยรวม Exception + LogWriter ไว้ด้วยกัน
This commit is contained in:
parent
f4b3eeac76
commit
2e77371316
5 changed files with 744 additions and 98 deletions
12
.gitignore
vendored
12
.gitignore
vendored
|
|
@ -16,8 +16,18 @@
|
|||
# Mono auto generated files
|
||||
mono_crash.*
|
||||
|
||||
# JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
|
||||
.idea/
|
||||
.vs/
|
||||
*.iml
|
||||
*.ipr
|
||||
*.iws
|
||||
out/
|
||||
.idea_modules/
|
||||
atlassian-ide-plugin.xml
|
||||
com_crashlytics_export_strings.xml
|
||||
crashlytics.properties
|
||||
crashlytics-build.properties
|
||||
fabric.properties
|
||||
|
||||
# Build results
|
||||
[Dd]ebug/
|
||||
|
|
|
|||
|
|
@ -0,0 +1,550 @@
|
|||
using BMA.EHR.Domain.Common;
|
||||
using BMA.EHR.Domain.Shared;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Nest;
|
||||
using Newtonsoft.Json;
|
||||
using System.Diagnostics;
|
||||
using System.Net;
|
||||
using System.Net.Http.Headers;
|
||||
using System.Security.Claims;
|
||||
using System.Text.Encodings.Web;
|
||||
using System.Text.Json;
|
||||
using JsonSerializer = System.Text.Json.JsonSerializer;
|
||||
|
||||
namespace BMA.EHR.Domain.Middlewares
|
||||
{
|
||||
public class CombinedErrorHandlerAndLoggingMiddleware
|
||||
{
|
||||
private readonly RequestDelegate _next;
|
||||
private readonly IConfiguration _configuration;
|
||||
|
||||
private string Uri = "";
|
||||
private string IndexFormat = "";
|
||||
private string SystemName = "";
|
||||
private string APIKey = "";
|
||||
|
||||
public CombinedErrorHandlerAndLoggingMiddleware(RequestDelegate next, IConfiguration configuration)
|
||||
{
|
||||
_next = next;
|
||||
_configuration = configuration;
|
||||
|
||||
Uri = _configuration["ElasticConfiguration:Uri"] ?? "http://192.168.1.40:9200";
|
||||
IndexFormat = _configuration["ElasticConfiguration:IndexFormat"] ?? "bma-ehr-log-index";
|
||||
SystemName = _configuration["ElasticConfiguration:SystemName"] ?? "Unknown";
|
||||
}
|
||||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
Console.WriteLine("=== CombinedErrorHandlerAndLoggingMiddleware Start ===");
|
||||
|
||||
var settings = new ConnectionSettings(new Uri(Uri))
|
||||
.DefaultIndex(IndexFormat);
|
||||
var client = new ElasticClient(settings);
|
||||
|
||||
var startTime = DateTime.UtcNow;
|
||||
var stopwatch = Stopwatch.StartNew();
|
||||
string? responseBodyJson = null;
|
||||
string? requestBodyJson = null;
|
||||
Exception? caughtException = null;
|
||||
|
||||
// อ่าน Request Body
|
||||
string requestBody = await ReadRequestBodyAsync(context);
|
||||
if (requestBody != "")
|
||||
{
|
||||
requestBodyJson = await FormatRequestBody(context, requestBody);
|
||||
}
|
||||
|
||||
var originalBodyStream = context.Response.Body;
|
||||
|
||||
using (var memoryStream = new MemoryStream())
|
||||
{
|
||||
// เปลี่ยน stream ของ Response เพื่อให้สามารถอ่านได้
|
||||
context.Response.Body = memoryStream;
|
||||
|
||||
var keycloakId = context.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value ?? Guid.Empty.ToString("D");
|
||||
var token = context.Request.Headers["Authorization"];
|
||||
GetProfileByKeycloakIdLocal? pf = null;
|
||||
|
||||
try
|
||||
{
|
||||
pf = await GetProfileByKeycloakIdAsync(Guid.Parse(keycloakId), token);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error getting profile: {ex.Message}");
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
Console.WriteLine($"Request completed with status: {context.Response.StatusCode}");
|
||||
|
||||
// จัดการ response format หลังจาก request ผ่าน pipeline แล้ว
|
||||
// รวมถึงการจัดการ 401/403 ที่มาจาก Authentication middleware
|
||||
await FormatResponse(context, memoryStream);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
Console.WriteLine("ObjectDisposedException caught in main Invoke");
|
||||
caughtException = new ObjectDisposedException("Response");
|
||||
return;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Console.WriteLine("OperationCanceledException caught in main Invoke");
|
||||
caughtException = new OperationCanceledException("Operation was cancelled");
|
||||
return;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
Console.WriteLine($"Exception caught in main Invoke: {error.Message}");
|
||||
caughtException = error;
|
||||
// จัดการ exception และ format เป็น response
|
||||
await FormatExceptionResponse(context, error, memoryStream);
|
||||
}
|
||||
finally
|
||||
{
|
||||
stopwatch.Stop();
|
||||
await LogRequest(context, client, startTime, stopwatch, pf, keycloakId, requestBodyJson, memoryStream, caughtException);
|
||||
|
||||
// เขียนข้อมูลกลับไปยัง original Response body
|
||||
if (memoryStream.Length > 0)
|
||||
{
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
await memoryStream.CopyToAsync(originalBodyStream);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Console.WriteLine("=== CombinedErrorHandlerAndLoggingMiddleware End ===");
|
||||
}
|
||||
|
||||
private async Task<string> FormatRequestBody(HttpContext context, string requestBody)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (context.Request.HasFormContentType)
|
||||
{
|
||||
var form = await context.Request.ReadFormAsync();
|
||||
var formData = new Dictionary<string, object>();
|
||||
|
||||
foreach (var field in form)
|
||||
{
|
||||
formData[field.Key] = field.Value.ToString();
|
||||
}
|
||||
|
||||
if (form.Files.Count > 0)
|
||||
{
|
||||
var fileDataList = new List<object>();
|
||||
foreach (var file in form.Files)
|
||||
{
|
||||
fileDataList.Add(new
|
||||
{
|
||||
FileName = file.FileName,
|
||||
ContentType = file.ContentType,
|
||||
Size = file.Length
|
||||
});
|
||||
}
|
||||
formData["Files"] = fileDataList;
|
||||
}
|
||||
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
WriteIndented = true,
|
||||
Converters = { new DateTimeFixConverter() }
|
||||
};
|
||||
return JsonSerializer.Serialize(formData, jsonOptions);
|
||||
}
|
||||
else
|
||||
{
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
WriteIndented = true,
|
||||
Converters = { new DateTimeFixConverter() }
|
||||
};
|
||||
return JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(requestBody), jsonOptions);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error formatting request body: {ex.Message}");
|
||||
return requestBody;
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task FormatResponse(HttpContext context, MemoryStream memoryStream)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (context?.Response == null)
|
||||
return;
|
||||
|
||||
var response = context.Response;
|
||||
var statusCode = response.StatusCode;
|
||||
|
||||
Console.WriteLine($"FormatResponse: StatusCode={statusCode}, HasStarted={response.HasStarted}");
|
||||
|
||||
// จัดการ response format แม้กับ status code จาก Authentication middleware
|
||||
if (!response.HasStarted && ShouldFormatResponse(statusCode))
|
||||
{
|
||||
Console.WriteLine($"Formatting response for status: {statusCode}");
|
||||
|
||||
var responseModel = CreateResponseModel(statusCode);
|
||||
|
||||
// Clear memory stream และเขียน response ใหม่
|
||||
memoryStream.SetLength(0);
|
||||
memoryStream.Position = 0;
|
||||
|
||||
// ไม่เปลี่ยน status code ที่ Authentication middleware ตั้งไว้
|
||||
response.ContentType = "application/json; charset=utf-8";
|
||||
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false
|
||||
};
|
||||
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
||||
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
||||
|
||||
Console.WriteLine($"Response formatted successfully: {jsonResponse}");
|
||||
}
|
||||
// หากเป็น 401/403 แต่ยังไม่มี response body ให้สร้างใหม่
|
||||
else if (!response.HasStarted && (statusCode == 401 || statusCode == 403) && memoryStream.Length == 0)
|
||||
{
|
||||
Console.WriteLine($"Creating response body for {statusCode} status");
|
||||
|
||||
var responseModel = CreateResponseModel(statusCode);
|
||||
response.ContentType = "application/json; charset=utf-8";
|
||||
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false
|
||||
};
|
||||
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
||||
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
||||
|
||||
Console.WriteLine($"Response body created: {jsonResponse}");
|
||||
}
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
Console.WriteLine("ObjectDisposedException in FormatResponse");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error in FormatResponse: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task FormatExceptionResponse(HttpContext context, Exception error, MemoryStream memoryStream)
|
||||
{
|
||||
try
|
||||
{
|
||||
Console.WriteLine($"FormatExceptionResponse: Error={error.Message}");
|
||||
|
||||
if (context?.Response == null)
|
||||
return;
|
||||
|
||||
var response = context.Response;
|
||||
Console.WriteLine($"Response HasStarted: {response.HasStarted}");
|
||||
|
||||
if (!response.HasStarted)
|
||||
{
|
||||
// Clear memory stream และเขียน error response
|
||||
memoryStream.SetLength(0);
|
||||
memoryStream.Position = 0;
|
||||
|
||||
response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
response.ContentType = "application/json; charset=utf-8";
|
||||
|
||||
var responseModel = new ResponseObject
|
||||
{
|
||||
Status = response.StatusCode,
|
||||
Message = GlobalMessages.ExceptionOccured,
|
||||
Result = GetErrorMessage(error)
|
||||
};
|
||||
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
WriteIndented = false
|
||||
};
|
||||
var jsonResponse = JsonSerializer.Serialize(responseModel, jsonOptions);
|
||||
var bytes = System.Text.Encoding.UTF8.GetBytes(jsonResponse);
|
||||
await memoryStream.WriteAsync(bytes, 0, bytes.Length);
|
||||
|
||||
Console.WriteLine($"Exception response formatted: {jsonResponse}");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Cannot format exception response - response already started");
|
||||
}
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
Console.WriteLine("ObjectDisposedException in FormatExceptionResponse");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error in FormatExceptionResponse: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private async Task LogRequest(HttpContext context, ElasticClient client, DateTime startTime, Stopwatch stopwatch,
|
||||
GetProfileByKeycloakIdLocal? pf, string keycloakId, string? requestBodyJson, MemoryStream memoryStream, Exception? caughtException)
|
||||
{
|
||||
try
|
||||
{
|
||||
var processTime = stopwatch.ElapsedMilliseconds;
|
||||
var endTime = DateTime.UtcNow;
|
||||
|
||||
var statusCode = caughtException != null ? (int)HttpStatusCode.InternalServerError : context.Response.StatusCode;
|
||||
|
||||
var logType = caughtException != null ? "error" : statusCode switch
|
||||
{
|
||||
>= 500 => "error",
|
||||
>= 400 => "warning",
|
||||
_ => "info"
|
||||
};
|
||||
|
||||
string? message = null;
|
||||
string? responseBodyJson = null;
|
||||
|
||||
// อ่านข้อมูลจาก Response
|
||||
if (memoryStream.Length > 0)
|
||||
{
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
var responseBody = new StreamReader(memoryStream).ReadToEnd();
|
||||
|
||||
if (responseBody != "")
|
||||
{
|
||||
var contentType = context.Response.ContentType;
|
||||
var isFileResponse = !contentType.StartsWith("application/json") && (
|
||||
contentType.StartsWith("application/") ||
|
||||
contentType.StartsWith("image/") ||
|
||||
contentType.StartsWith("audio/") ||
|
||||
context.Response.Headers.ContainsKey("Content-Disposition")
|
||||
);
|
||||
|
||||
if (isFileResponse)
|
||||
{
|
||||
responseBodyJson = "";
|
||||
message = "success";
|
||||
}
|
||||
else
|
||||
{
|
||||
try
|
||||
{
|
||||
var jsonOptions = new JsonSerializerOptions
|
||||
{
|
||||
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
||||
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
|
||||
WriteIndented = true,
|
||||
Converters = { new DateTimeFixConverter() }
|
||||
};
|
||||
responseBodyJson = JsonSerializer.Serialize(JsonSerializer.Deserialize<object>(responseBody), jsonOptions);
|
||||
|
||||
var json = JsonSerializer.Deserialize<JsonElement>(responseBody);
|
||||
if (json.ValueKind == JsonValueKind.Array)
|
||||
{
|
||||
message = "success";
|
||||
}
|
||||
else
|
||||
{
|
||||
if (json.TryGetProperty("message", out var messageElement))
|
||||
{
|
||||
message = messageElement.GetString();
|
||||
}
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
responseBodyJson = responseBody;
|
||||
message = caughtException?.Message ?? "Unknown error";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (caughtException != null)
|
||||
{
|
||||
message = caughtException.Message;
|
||||
}
|
||||
|
||||
var logData = new
|
||||
{
|
||||
logType = logType,
|
||||
ip = context.Connection.RemoteIpAddress?.ToString(),
|
||||
rootId = pf?.RootId,
|
||||
systemName = SystemName,
|
||||
startTimeStamp = startTime.ToString("o"),
|
||||
endTimeStamp = endTime.ToString("o"),
|
||||
processTime = processTime,
|
||||
host = context.Request.Host.Value,
|
||||
method = context.Request.Method,
|
||||
endpoint = context.Request.Path + context.Request.QueryString,
|
||||
responseCode = statusCode == 304 ? "200" : statusCode.ToString(),
|
||||
responseDescription = message,
|
||||
input = requestBodyJson,
|
||||
output = responseBodyJson,
|
||||
userId = keycloakId,
|
||||
userName = $"{pf?.Prefix ?? ""}{pf?.FirstName ?? ""} {pf?.LastName ?? ""}",
|
||||
user = pf?.CitizenId ?? "",
|
||||
exception = caughtException?.ToString()
|
||||
};
|
||||
|
||||
client.IndexDocument(logData);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error logging request: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ShouldFormatResponse(int statusCode)
|
||||
{
|
||||
return statusCode == (int)HttpStatusCode.Unauthorized ||
|
||||
statusCode == (int)HttpStatusCode.Forbidden ||
|
||||
statusCode == (int)HttpStatusCode.BadRequest ||
|
||||
statusCode == (int)HttpStatusCode.NotFound ||
|
||||
statusCode == (int)HttpStatusCode.Conflict ||
|
||||
statusCode == (int)HttpStatusCode.UnprocessableEntity ||
|
||||
statusCode == (int)HttpStatusCode.InternalServerError;
|
||||
}
|
||||
|
||||
private static ResponseObject CreateResponseModel(int statusCode)
|
||||
{
|
||||
var message = statusCode switch
|
||||
{
|
||||
(int)HttpStatusCode.Unauthorized => GlobalMessages.NotAuthorized,
|
||||
(int)HttpStatusCode.Forbidden => GlobalMessages.ForbiddenAccess,
|
||||
(int)HttpStatusCode.BadRequest => "Bad Request",
|
||||
(int)HttpStatusCode.NotFound => "Resource Not Found",
|
||||
(int)HttpStatusCode.Conflict => "Conflict",
|
||||
(int)HttpStatusCode.UnprocessableEntity => "Validation Error",
|
||||
(int)HttpStatusCode.InternalServerError => GlobalMessages.ExceptionOccured,
|
||||
_ => "Error"
|
||||
};
|
||||
|
||||
return new ResponseObject
|
||||
{
|
||||
Status = statusCode,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetErrorMessage(Exception error)
|
||||
{
|
||||
var msg = error.Message;
|
||||
var inner = error.InnerException;
|
||||
while (inner != null)
|
||||
{
|
||||
msg += $" {inner.Message}\r\n";
|
||||
inner = inner.InnerException;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
protected async Task<string> GetExternalAPIAsync(string apiPath, string accessToken, string apiKey)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (var client = new HttpClient())
|
||||
{
|
||||
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken.Replace("Bearer ", ""));
|
||||
client.DefaultRequestHeaders.Add("api_key", apiKey);
|
||||
var _res = await client.GetAsync(apiPath);
|
||||
if (_res.IsSuccessStatusCode)
|
||||
{
|
||||
var _result = await _res.Content.ReadAsStringAsync();
|
||||
return _result;
|
||||
}
|
||||
return string.Empty;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<GetProfileByKeycloakIdLocal?> GetProfileByKeycloakIdAsync(Guid keycloakId, string? accessToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
var apiPath = $"{_configuration["API"]}/org/dotnet/keycloak/{keycloakId}";
|
||||
var apiKey = _configuration["API_KEY"];
|
||||
|
||||
var apiResult = await GetExternalAPIAsync(apiPath, accessToken ?? "", apiKey);
|
||||
if (apiResult != null)
|
||||
{
|
||||
var raw = JsonConvert.DeserializeObject<GetProfileByKeycloakIdResultLocal>(apiResult);
|
||||
if (raw != null)
|
||||
return raw.Result;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
catch
|
||||
{
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
private async Task<string> ReadRequestBodyAsync(HttpContext context)
|
||||
{
|
||||
context.Request.EnableBuffering();
|
||||
using var reader = new StreamReader(context.Request.Body, leaveOpen: true);
|
||||
var body = await reader.ReadToEndAsync();
|
||||
context.Request.Body.Position = 0;
|
||||
return body;
|
||||
}
|
||||
}
|
||||
|
||||
// Model classes (ถ้ายังไม่มีใน namespace นี้)
|
||||
public class GetProfileByKeycloakIdLocal
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public string? Prefix { get; set; }
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? CitizenId { get; set; }
|
||||
public string? Root { get; set; }
|
||||
public string? Child1 { get; set; }
|
||||
public string? Child2 { get; set; }
|
||||
public string? Child3 { get; set; }
|
||||
public string? Child4 { get; set; }
|
||||
public Guid? RootId { get; set; }
|
||||
public Guid? Child1Id { get; set; }
|
||||
public Guid? Child2Id { get; set; }
|
||||
public Guid? Child3Id { get; set; }
|
||||
public Guid? Child4Id { get; set; }
|
||||
public Guid? RootDnaId { get; set; }
|
||||
public Guid? Child1DnaId { get; set; }
|
||||
public Guid? Child2DnaId { get; set; }
|
||||
public Guid? Child3DnaId { get; set; }
|
||||
public Guid? Child4DnaId { get; set; }
|
||||
public double? Amount { get; set; }
|
||||
public double? PositionSalaryAmount { get; set; }
|
||||
public string? Commander { get; set; }
|
||||
public Guid? CommanderId { get; set; }
|
||||
public Guid? CommanderKeycloak { get; set; }
|
||||
}
|
||||
|
||||
public class GetProfileByKeycloakIdResultLocal
|
||||
{
|
||||
public string Message { get; set; } = string.Empty;
|
||||
public int Status { get; set; } = -1;
|
||||
public GetProfileByKeycloakIdLocal? Result { get; set; }
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -17,85 +17,166 @@ namespace BMA.EHR.Domain.Middlewares
|
|||
|
||||
public async Task Invoke(HttpContext context)
|
||||
{
|
||||
Console.WriteLine("=== ErrorHandlerMiddleware Start ===");
|
||||
|
||||
try
|
||||
{
|
||||
await _next(context);
|
||||
Console.WriteLine($"Request completed with status: {context.Response.StatusCode}");
|
||||
|
||||
// จัดการ response format หลังจาก request ผ่าน pipeline แล้ว
|
||||
await FormatResponse(context);
|
||||
}
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
Console.WriteLine("ObjectDisposedException caught in main Invoke");
|
||||
return;
|
||||
}
|
||||
catch (OperationCanceledException)
|
||||
{
|
||||
Console.WriteLine("OperationCanceledException caught in main Invoke");
|
||||
return;
|
||||
}
|
||||
catch (Exception error)
|
||||
{
|
||||
Console.WriteLine($"Exception caught in main Invoke: {error.Message}");
|
||||
// จัดการ exception และ format เป็น response
|
||||
await FormatExceptionResponse(context, error);
|
||||
}
|
||||
|
||||
Console.WriteLine("=== ErrorHandlerMiddleware End ===");
|
||||
}
|
||||
|
||||
private static async Task FormatResponse(HttpContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (context?.Response == null)
|
||||
return;
|
||||
|
||||
var response = context.Response;
|
||||
var statusCode = response.StatusCode;
|
||||
|
||||
// ตรวจสอบว่า response ยังไม่ถูกส่งและเป็น status code ที่ต้องการจัดการ
|
||||
if (!response.HasStarted &&
|
||||
(statusCode == (int)HttpStatusCode.Unauthorized || statusCode == (int)HttpStatusCode.Forbidden))
|
||||
{
|
||||
var responseModel = new ResponseObject
|
||||
{
|
||||
Status = statusCode,
|
||||
Message = statusCode == (int)HttpStatusCode.Unauthorized
|
||||
? GlobalMessages.NotAuthorized
|
||||
: GlobalMessages.ForbiddenAccess
|
||||
};
|
||||
// Debug log
|
||||
Console.WriteLine($"FormatResponse: StatusCode={statusCode}, HasStarted={response.HasStarted}");
|
||||
|
||||
response.ContentType = "application/json";
|
||||
// จัดการเฉพาะ status code ที่ต้องการ format และ response ยังไม่เริ่ม
|
||||
if (!response.HasStarted && ShouldFormatResponse(statusCode))
|
||||
{
|
||||
Console.WriteLine($"Formatting response for status: {statusCode}");
|
||||
|
||||
var responseModel = CreateResponseModel(statusCode);
|
||||
|
||||
// Clear และตั้งค่าใหม่
|
||||
response.Clear();
|
||||
response.StatusCode = statusCode; // เซ็ตกลับไป
|
||||
response.ContentType = "application/json; charset=utf-8";
|
||||
|
||||
// ใช้ JsonSerializer แทน WriteAsJsonAsync เพื่อความปลอดภัย
|
||||
var jsonResponse = JsonSerializer.Serialize(responseModel);
|
||||
await response.WriteAsync(jsonResponse);
|
||||
|
||||
Console.WriteLine($"Response formatted successfully: {jsonResponse}");
|
||||
}
|
||||
}
|
||||
catch (Exception error)
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
await HandleExceptionAsync(context, error);
|
||||
Console.WriteLine("ObjectDisposedException in FormatResponse");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error in FormatResponse: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static async Task HandleExceptionAsync(HttpContext context, Exception error)
|
||||
private static async Task FormatExceptionResponse(HttpContext context, Exception error)
|
||||
{
|
||||
var response = context.Response;
|
||||
|
||||
// ตรวจสอบว่า response ยังไม่ถูกส่งไป
|
||||
if (response.HasStarted)
|
||||
{
|
||||
// ถ้า response เริ่มแล้ว ไม่สามารถแก้ไขได้ แค่ log
|
||||
Console.WriteLine("Cannot write error response, stream already started.");
|
||||
Console.WriteLine($"Error: {error}");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Clear response เฉพาะเมื่อยังไม่ได้เริ่มส่ง
|
||||
response.Clear();
|
||||
response.ContentType = "application/json";
|
||||
response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
Console.WriteLine($"FormatExceptionResponse: Error={error.Message}");
|
||||
|
||||
if (context?.Response == null)
|
||||
return;
|
||||
|
||||
// สร้าง error message
|
||||
var msg = error.Message;
|
||||
var inner = error.InnerException;
|
||||
while (inner != null)
|
||||
var response = context.Response;
|
||||
Console.WriteLine($"Response HasStarted: {response.HasStarted}");
|
||||
|
||||
if (!response.HasStarted)
|
||||
{
|
||||
msg += $" {inner.Message}\r\n";
|
||||
inner = inner.InnerException;
|
||||
// Clear และตั้งค่า response
|
||||
response.Clear();
|
||||
response.StatusCode = (int)HttpStatusCode.InternalServerError;
|
||||
response.ContentType = "application/json; charset=utf-8";
|
||||
|
||||
var responseModel = new ResponseObject
|
||||
{
|
||||
Status = response.StatusCode,
|
||||
Message = GlobalMessages.ExceptionOccured,
|
||||
Result = GetErrorMessage(error)
|
||||
};
|
||||
|
||||
var jsonResponse = JsonSerializer.Serialize(responseModel);
|
||||
await response.WriteAsync(jsonResponse);
|
||||
|
||||
Console.WriteLine($"Exception response formatted: {jsonResponse}");
|
||||
}
|
||||
|
||||
var responseModel = new ResponseObject
|
||||
else
|
||||
{
|
||||
Status = response.StatusCode,
|
||||
Message = GlobalMessages.ExceptionOccured,
|
||||
Result = msg
|
||||
};
|
||||
|
||||
// ใช้ JsonSerializer และ WriteAsync เพื่อหลีกเลี่ยงปัญหา stream
|
||||
var jsonResponse = JsonSerializer.Serialize(responseModel);
|
||||
await response.WriteAsync(jsonResponse);
|
||||
Console.WriteLine("Cannot format exception response - response already started");
|
||||
}
|
||||
}
|
||||
catch (Exception writeError)
|
||||
catch (ObjectDisposedException)
|
||||
{
|
||||
// ถ้าเขียน response ไม่ได้ ให้ log error
|
||||
Console.WriteLine("Failed to write error response:");
|
||||
Console.WriteLine($"Original Error: {error}");
|
||||
Console.WriteLine($"Write Error: {writeError}");
|
||||
Console.WriteLine("ObjectDisposedException in FormatExceptionResponse");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error in FormatExceptionResponse: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
private static bool ShouldFormatResponse(int statusCode)
|
||||
{
|
||||
// กำหนด status code ที่ต้องการ format
|
||||
return statusCode == (int)HttpStatusCode.Unauthorized ||
|
||||
statusCode == (int)HttpStatusCode.Forbidden ||
|
||||
statusCode == (int)HttpStatusCode.BadRequest ||
|
||||
statusCode == (int)HttpStatusCode.NotFound ||
|
||||
statusCode == (int)HttpStatusCode.Conflict ||
|
||||
statusCode == (int)HttpStatusCode.UnprocessableEntity ||
|
||||
statusCode == (int)HttpStatusCode.InternalServerError; // 500
|
||||
}
|
||||
|
||||
private static ResponseObject CreateResponseModel(int statusCode)
|
||||
{
|
||||
var message = statusCode switch
|
||||
{
|
||||
(int)HttpStatusCode.Unauthorized => GlobalMessages.NotAuthorized,
|
||||
(int)HttpStatusCode.Forbidden => GlobalMessages.ForbiddenAccess,
|
||||
(int)HttpStatusCode.BadRequest => "Bad Request",
|
||||
(int)HttpStatusCode.NotFound => "Resource Not Found",
|
||||
(int)HttpStatusCode.Conflict => "Conflict",
|
||||
(int)HttpStatusCode.UnprocessableEntity => "Validation Error",
|
||||
(int)HttpStatusCode.InternalServerError => GlobalMessages.ExceptionOccured,
|
||||
_ => "Error"
|
||||
};
|
||||
|
||||
return new ResponseObject
|
||||
{
|
||||
Status = statusCode,
|
||||
Message = message
|
||||
};
|
||||
}
|
||||
|
||||
private static string GetErrorMessage(Exception error)
|
||||
{
|
||||
var msg = error.Message;
|
||||
var inner = error.InnerException;
|
||||
while (inner != null)
|
||||
{
|
||||
msg += $" {inner.Message}\r\n";
|
||||
inner = inner.InnerException;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -247,46 +247,46 @@ namespace BMA.EHR.Domain.Middlewares
|
|||
}
|
||||
}
|
||||
|
||||
public class GetProfileByKeycloakIdLocal
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
|
||||
public string? Prefix { get; set; }
|
||||
public string? FirstName { get; set; }
|
||||
public string? LastName { get; set; }
|
||||
public string? CitizenId { get; set; }
|
||||
|
||||
public string? Root { get; set; }
|
||||
public string? Child1 { get; set; }
|
||||
public string? Child2 { get; set; }
|
||||
public string? Child3 { get; set; }
|
||||
public string? Child4 { get; set; }
|
||||
public Guid? RootId { get; set; }
|
||||
public Guid? Child1Id { get; set; }
|
||||
public Guid? Child2Id { get; set; }
|
||||
public Guid? Child3Id { get; set; }
|
||||
public Guid? Child4Id { get; set; }
|
||||
public Guid? RootDnaId { get; set; }
|
||||
public Guid? Child1DnaId { get; set; }
|
||||
public Guid? Child2DnaId { get; set; }
|
||||
public Guid? Child3DnaId { get; set; }
|
||||
public Guid? Child4DnaId { get; set; }
|
||||
public double? Amount { get; set; }
|
||||
public double? PositionSalaryAmount { get; set; }
|
||||
public string? Commander { get; set; }
|
||||
|
||||
public Guid? CommanderId { get; set; }
|
||||
|
||||
public Guid? CommanderKeycloak { get; set; }
|
||||
|
||||
}
|
||||
|
||||
public class GetProfileByKeycloakIdResultLocal
|
||||
{
|
||||
public string Message { get; set; } = string.Empty;
|
||||
|
||||
public int Status { get; set; } = -1;
|
||||
|
||||
public GetProfileByKeycloakIdLocal? Result { get; set; }
|
||||
}
|
||||
// public class GetProfileByKeycloakIdLocal
|
||||
// {
|
||||
// public Guid Id { get; set; }
|
||||
//
|
||||
// public string? Prefix { get; set; }
|
||||
// public string? FirstName { get; set; }
|
||||
// public string? LastName { get; set; }
|
||||
// public string? CitizenId { get; set; }
|
||||
//
|
||||
// public string? Root { get; set; }
|
||||
// public string? Child1 { get; set; }
|
||||
// public string? Child2 { get; set; }
|
||||
// public string? Child3 { get; set; }
|
||||
// public string? Child4 { get; set; }
|
||||
// public Guid? RootId { get; set; }
|
||||
// public Guid? Child1Id { get; set; }
|
||||
// public Guid? Child2Id { get; set; }
|
||||
// public Guid? Child3Id { get; set; }
|
||||
// public Guid? Child4Id { get; set; }
|
||||
// public Guid? RootDnaId { get; set; }
|
||||
// public Guid? Child1DnaId { get; set; }
|
||||
// public Guid? Child2DnaId { get; set; }
|
||||
// public Guid? Child3DnaId { get; set; }
|
||||
// public Guid? Child4DnaId { get; set; }
|
||||
// public double? Amount { get; set; }
|
||||
// public double? PositionSalaryAmount { get; set; }
|
||||
// public string? Commander { get; set; }
|
||||
//
|
||||
// public Guid? CommanderId { get; set; }
|
||||
//
|
||||
// public Guid? CommanderKeycloak { get; set; }
|
||||
//
|
||||
// }
|
||||
//
|
||||
// public class GetProfileByKeycloakIdResultLocal
|
||||
// {
|
||||
// public string Message { get; set; } = string.Empty;
|
||||
//
|
||||
// public int Status { get; set; } = -1;
|
||||
//
|
||||
// public GetProfileByKeycloakIdLocal? Result { get; set; }
|
||||
// }
|
||||
}
|
||||
|
|
|
|||
|
|
@ -155,14 +155,19 @@ app.MapHealthChecks("/health");
|
|||
|
||||
app.UseHttpsRedirection();
|
||||
app.UseCors();
|
||||
|
||||
app.UseMiddleware<CombinedErrorHandlerAndLoggingMiddleware>();
|
||||
|
||||
app.UseAuthentication();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.UseDefaultFiles();
|
||||
app.UseStaticFiles();
|
||||
app.MapControllers();
|
||||
app.UseMiddleware<ErrorHandlerMiddleware>();
|
||||
// Disable ก่อน เพื่อแก้ไขให้เรีบร้อยก่อนการใช้งาน
|
||||
app.UseMiddleware<RequestLoggingMiddleware>();
|
||||
// app.UseMiddleware<ErrorHandlerMiddleware>();
|
||||
// // Disable ก่อน เพื่อแก้ไขให้เรีบร้อยก่อนการใช้งาน
|
||||
// app.UseMiddleware<RequestLoggingMiddleware>();
|
||||
|
||||
|
||||
|
||||
app.UseHangfireDashboard("/hangfire", new DashboardOptions()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue