浏览代码

Error messages from the server are actually transported through client connection now.

Kenric Nugteren 1 年之前
父节点
当前提交
df97f56d2b

+ 14 - 6
InABox.Client.RPC/Transports/RPCClientTransport.cs

@@ -1,6 +1,7 @@
 using System;
 using System.Collections.Concurrent;
 using System.Collections.Generic;
+using System.Text;
 using System.Threading;
 using System.Threading.Tasks;
 using InABox.Clients;
@@ -92,12 +93,19 @@ namespace InABox.Rpc
             var response = GetResponse(request.Id, ev, DefaultRequestTimeout)
                 ?? throw new Exception($"{typeof(TCommand).Name}({request.Id}) returned NULL");
 
-            if (response.Error != RpcError.NONE) throw new RpcException($"Server error in {typeof(TCommand).Name}({request.Id}): {response.Error}", response.Error);
-            
-            var result = Serialization.ReadBinary<TResult>(response.Payload, BinarySerializationSettings.Latest)
-                ?? throw new Exception($"Cannot Deserialize {typeof(TCommand).Name}({request.Id})");
-            
-            return result;
+            switch (response.Error)
+            {
+                case RpcError.NONE:
+                    var result = Serialization.ReadBinary<TResult>(response.Payload, BinarySerializationSettings.Latest)
+                        ?? throw new Exception($"Cannot Deserialize {typeof(TCommand).Name}({request.Id})");
+
+                    return result;
+                case RpcError.SERVERERROR:
+                    var errorMessage = Encoding.UTF8.GetString(response.Payload);
+                    throw new RpcException(errorMessage, response.Error);
+                default:
+                    throw new RpcException($"Server error in {typeof(TCommand).Name}({request.Id}): {response.Error}", response.Error);
+            }
         }
 
         private void CheckConnection<TCommand, TParameters, TResult>() where TCommand : IRpcCommand<TParameters, TResult>

+ 2 - 2
InABox.Core/DigitalForms/Forms/DigitalFormCategoryLookups.cs

@@ -37,8 +37,8 @@ namespace InABox.Core
         public DigitalFormCategoryLookups(object[]? items) : base(items)
         {
             var lookups = Lookups();
-            foreach (var key in lookups.Keys)
-                AddValue(key, lookups[key]);
+            foreach (var (key, value) in lookups)
+                AddValue(key, value);
         }
     }
 }

+ 3 - 0
InABox.Server/RPC/Transports/RPCServerTransport.cs

@@ -1,4 +1,5 @@
 using System.Collections.Concurrent;
+using System.Text;
 using InABox.Core;
 
 namespace InABox.Rpc
@@ -112,6 +113,7 @@ namespace InABox.Rpc
                         }
                         catch (RpcException err)
                         {
+                            response.Payload = Encoding.UTF8.GetBytes(err.Message);
                             response.Error = err.Error;
                         }
                     }
@@ -132,6 +134,7 @@ namespace InABox.Rpc
             catch(Exception e)
             {
                 DoException(connection, e);
+                response.Payload = Encoding.UTF8.GetBytes(e.Message);
                 response.Error = RpcError.SERVERERROR;
             }