NewLife/NewLife.Remoting

[fix] 修正ApiFilterAttribute.OnActionExecuted对异常的包装处理,解决星尘接口异常问题
智能大石头 authored at 2025-09-16 00:34:57
203e9bd
Tree
1 Parent(s) 1139d1e
Summary: 2 changed files with 20 additions and 15 deletions.
Modified +18 -14
Modified +2 -1
Modified +18 -14
diff --git a/NewLife.Remoting.Extensions/ApiFilterAttribute.cs b/NewLife.Remoting.Extensions/ApiFilterAttribute.cs
index 835e37d..672d073 100644
--- a/NewLife.Remoting.Extensions/ApiFilterAttribute.cs
+++ b/NewLife.Remoting.Extensions/ApiFilterAttribute.cs
@@ -49,32 +49,36 @@ public sealed class ApiFilterAttribute : ActionFilterAttribute
         if (context.HttpContext.WebSockets.IsWebSocketRequest) return;
 
         if (context.Result != null)
+        {
             if (context.Result is ObjectResult obj)
+            {
                 context.Result = new JsonResult(new { code = obj.StatusCode ?? 0, data = obj.Value });
+            }
             else if (context.Result is EmptyResult)
             {
                 DefaultTracer.Instance?.NewSpan("apiFilter-EmptyResult");
                 context.Result = new JsonResult(new { code = 0, data = new { } });
             }
-            else if (context.Exception != null && !context.ExceptionHandled)
+        }
+        else if (context.Exception != null && !context.ExceptionHandled)
+        {
+            var ex = context.Exception.GetTrue();
+            if (ex is ApiException aex)
+                context.Result = new JsonResult(new { code = aex.Code, message = aex.Message });
+            else
             {
-                var ex = context.Exception.GetTrue();
-                if (ex is ApiException aex)
-                    context.Result = new JsonResult(new { code = aex.Code, message = aex.Message });
-                else
-                {
-                    context.Result = new JsonResult(new { code = 500, message = ex.Message });
+                context.Result = new JsonResult(new { code = 500, message = ex.Message });
 
-                    // 埋点拦截业务异常
-                    var action = context.HttpContext.Request.Path + "";
-                    if (context.ActionDescriptor is ControllerActionDescriptor act) action = $"/{act.ControllerName}/{act.ActionName}";
+                // 埋点拦截业务异常
+                var action = context.HttpContext.Request.Path + "";
+                if (context.ActionDescriptor is ControllerActionDescriptor act) action = $"/{act.ControllerName}/{act.ActionName}";
 
-                    DefaultTracer.Instance?.NewError(action, ex);
-                }
-
-                context.ExceptionHandled = true;
+                DefaultTracer.Instance?.NewError(action, ex);
             }
 
+            context.ExceptionHandled = true;
+        }
+
         base.OnActionExecuted(context);
     }
 }
\ No newline at end of file
Modified +2 -1
diff --git a/NewLife.Remoting.Extensions/Services/DefaultDeviceService.cs b/NewLife.Remoting.Extensions/Services/DefaultDeviceService.cs
index 57a4b73..7ef5499 100644
--- a/NewLife.Remoting.Extensions/Services/DefaultDeviceService.cs
+++ b/NewLife.Remoting.Extensions/Services/DefaultDeviceService.cs
@@ -292,7 +292,8 @@ public abstract class DefaultDeviceService<TDevice, TOnline>(ISessionManager ses
     /// <returns></returns>
     public virtual void SetOnline(DeviceContext context, Boolean online)
     {
-        var olt = context.Online ?? GetOnline(context);
+        //var olt = context.Online ?? GetOnline(context);
+        var olt = GetOnline(context);
         if (olt != null && olt is IEntity entity)
         {
             entity.SetItem("WebSocket", online);