diff --git a/.idea/.idea.BMA.EHR.Solution/.idea/workspace.xml b/.idea/.idea.BMA.EHR.Solution/.idea/workspace.xml
index 28b164fb..74b94d8b 100644
--- a/.idea/.idea.BMA.EHR.Solution/.idea/workspace.xml
+++ b/.idea/.idea.BMA.EHR.Solution/.idea/workspace.xml
@@ -39,14 +39,18 @@
-
+
+
+
+
+
-
+
@@ -57,6 +61,7 @@
+
{
"customColor": "",
"associatedIndex": 2
@@ -66,23 +71,23 @@
- {
- "keyToString": {
- ".NET Launch Settings Profile.BMA.EHR.Leave: https.executor": "Debug",
- ".NET Launch Settings Profile.BMA.EHR.Placement.Service: http.executor": "Run",
- "RunOnceActivity.ShowReadmeOnStart": "true",
- "RunOnceActivity.git.unshallow": "true",
- "git-widget-placeholder": "develop",
- "node.js.detected.package.eslint": "true",
- "node.js.detected.package.tslint": "true",
- "node.js.selected.package.eslint": "(autodetect)",
- "node.js.selected.package.tslint": "(autodetect)",
- "nodejs_package_manager_path": "npm",
- "settings.editor.selected.configurable": "preferences.lookFeel",
- "vue.rearranger.settings.migration": "true"
+
-
+}]]>
+
@@ -566,6 +571,7 @@
+
diff --git a/BMA.EHR.Application/Repositories/PermissionRepository.cs b/BMA.EHR.Application/Repositories/PermissionRepository.cs
index 27ee8696..9626a9fb 100644
--- a/BMA.EHR.Application/Repositories/PermissionRepository.cs
+++ b/BMA.EHR.Application/Repositories/PermissionRepository.cs
@@ -58,7 +58,8 @@ namespace BMA.EHR.Application.Repositories
using (var client = new HttpClient())
{
- client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
+ client.DefaultRequestHeaders.Authorization =
+ new AuthenticationHeaderValue("Bearer", AccessToken.Replace("Bearer ", ""));
client.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
var req = await client.GetAsync(apiPath);
var res = await req.Content.ReadAsStringAsync();
diff --git a/BMA.EHR.Domain/Middlewares/ErrorHandlerMiddleware.cs b/BMA.EHR.Domain/Middlewares/ErrorHandlerMiddleware.cs
index af1ce4fb..4e1bd79a 100644
--- a/BMA.EHR.Domain/Middlewares/ErrorHandlerMiddleware.cs
+++ b/BMA.EHR.Domain/Middlewares/ErrorHandlerMiddleware.cs
@@ -2,6 +2,7 @@
using BMA.EHR.Domain.Shared;
using Microsoft.AspNetCore.Http;
using System.Net;
+using System.Text.Json;
namespace BMA.EHR.Domain.Middlewares
{
@@ -23,6 +24,7 @@ namespace BMA.EHR.Domain.Middlewares
var response = context.Response;
var statusCode = response.StatusCode;
+ // ตรวจสอบว่า response ยังไม่ถูกส่งและเป็น status code ที่ต้องการจัดการ
if (!response.HasStarted &&
(statusCode == (int)HttpStatusCode.Unauthorized || statusCode == (int)HttpStatusCode.Forbidden))
{
@@ -35,43 +37,144 @@ namespace BMA.EHR.Domain.Middlewares
};
response.ContentType = "application/json";
- await response.WriteAsJsonAsync(responseModel);
+
+ // ใช้ JsonSerializer แทน WriteAsJsonAsync เพื่อความปลอดภัย
+ var jsonResponse = JsonSerializer.Serialize(responseModel);
+ await response.WriteAsync(jsonResponse);
}
}
catch (Exception error)
{
- var response = context.Response;
+ await HandleExceptionAsync(context, error);
+ }
+ }
- if (!response.HasStarted)
+ private static async Task HandleExceptionAsync(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;
+
+ // สร้าง error message
+ var msg = error.Message;
+ var inner = error.InnerException;
+ while (inner != null)
{
- response.Clear();
- response.ContentType = "application/json";
- response.StatusCode = (int)HttpStatusCode.InternalServerError;
-
- var msg = error.Message;
- var inner = error.InnerException;
- while (inner != null)
- {
- msg += $" {inner.Message}\r\n";
- inner = inner.InnerException;
- }
-
- var responseModel = new ResponseObject
- {
- Status = response.StatusCode,
- Message = GlobalMessages.ExceptionOccured,
- Result = msg
- };
-
- await response.WriteAsJsonAsync(responseModel);
+ msg += $" {inner.Message}\r\n";
+ inner = inner.InnerException;
}
- else
+
+ var responseModel = new ResponseObject
{
- // logging กรณีที่ response เริ่มถูกส่งแล้ว
- Console.WriteLine("Cannot write error response, stream already started.");
- Console.WriteLine(error);
- }
+ Status = response.StatusCode,
+ Message = GlobalMessages.ExceptionOccured,
+ Result = msg
+ };
+
+ // ใช้ JsonSerializer และ WriteAsync เพื่อหลีกเลี่ยงปัญหา stream
+ var jsonResponse = JsonSerializer.Serialize(responseModel);
+ await response.WriteAsync(jsonResponse);
+ }
+ catch (Exception writeError)
+ {
+ // ถ้าเขียน response ไม่ได้ ให้ log error
+ Console.WriteLine("Failed to write error response:");
+ Console.WriteLine($"Original Error: {error}");
+ Console.WriteLine($"Write Error: {writeError}");
}
}
}
}
+
+
+// using BMA.EHR.Domain.Common;
+// using BMA.EHR.Domain.Shared;
+// using Microsoft.AspNetCore.Http;
+// using System.Net;
+//
+// namespace BMA.EHR.Domain.Middlewares
+// {
+// public class ErrorHandlerMiddleware
+// {
+// private readonly RequestDelegate _next;
+//
+// public ErrorHandlerMiddleware(RequestDelegate next)
+// {
+// _next = next;
+// }
+//
+// public async Task Invoke(HttpContext context)
+// {
+// try
+// {
+// await _next(context);
+//
+// var response = context.Response;
+// var statusCode = response.StatusCode;
+//
+// 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
+// };
+//
+// response.ContentType = "application/json";
+// await response.WriteAsJsonAsync(responseModel);
+// }
+// }
+// catch (Exception error)
+// {
+// var response = context.Response;
+//
+// if (!response.HasStarted)
+// {
+// response.Clear();
+// response.ContentType = "application/json";
+// response.StatusCode = (int)HttpStatusCode.InternalServerError;
+//
+// var msg = error.Message;
+// var inner = error.InnerException;
+// while (inner != null)
+// {
+// msg += $" {inner.Message}\r\n";
+// inner = inner.InnerException;
+// }
+//
+// var responseModel = new ResponseObject
+// {
+// Status = response.StatusCode,
+// Message = GlobalMessages.ExceptionOccured,
+// Result = msg
+// };
+//
+// await response.WriteAsJsonAsync(responseModel);
+// }
+// else
+// {
+// // logging กรณีที่ response เริ่มถูกส่งแล้ว
+// Console.WriteLine("Cannot write error response, stream already started.");
+// Console.WriteLine(error);
+// }
+// }
+// }
+// }
+// }