11using System . Diagnostics . CodeAnalysis ;
22using System . Text ;
33using Newtonsoft . Json ;
4+ using Newtonsoft . Json . Linq ;
45
56namespace UiPath . Ipc ;
67
@@ -48,17 +49,40 @@ public TResult Deserialize<TResult>()
4849 }
4950}
5051
51- public record Error ( string Message , string StackTrace , string Type , Error ? InnerError )
52+ public record Error ( string Message , string StackTrace , string Type , Error ? InnerError , IReadOnlyDictionary < string , object ? > ? Data )
5253{
54+ public static readonly HashSet < string > SerializableDataKeys = [ "UiPath.ErrorInfo.Error" ] ;
55+
5356 [ return : NotNullIfNotNull ( "exception" ) ]
5457 public static Error ? FromException ( Exception ? exception )
55- => exception is null
56- ? null
58+ => exception is null
59+ ? null
5760 : new (
58- Message : exception . Message ,
59- StackTrace : exception . StackTrace ?? exception . GetBaseException ( ) . StackTrace ! ,
60- Type : GetExceptionType ( exception ) ,
61- InnerError : FromException ( exception . InnerException ) ) ;
61+ Message : exception . Message ,
62+ StackTrace : exception . StackTrace ?? exception . GetBaseException ( ) . StackTrace ! ,
63+ Type : GetExceptionType ( exception ) ,
64+ InnerError : FromException ( exception . InnerException ) ,
65+ Data : GetExceptionData ( exception ) ) ;
66+
67+ private static IReadOnlyDictionary < string , object ? > ? GetExceptionData ( Exception exception )
68+ {
69+ Dictionary < string , object ? > ? data = null ;
70+ foreach ( var key in SerializableDataKeys )
71+ {
72+ if ( exception . Data . Contains ( key ) )
73+ {
74+ data ??= [ ] ;
75+ var value = exception . Data [ key ] ;
76+ data [ key ] = value switch
77+ {
78+ null or string or int or bool or Int64 or double or decimal or float => value ,
79+ _ => JToken . FromObject ( value , IpcJsonSerializer . StringArgsSerializer )
80+ } ;
81+ }
82+ }
83+ return data ;
84+ }
85+
6286 public override string ToString ( ) => new RemoteException ( this ) . ToString ( ) ;
6387
6488 private static string GetExceptionType ( Exception exception ) => ( exception as RemoteException ) ? . Type ?? exception . GetType ( ) . FullName ! ;
@@ -70,6 +94,14 @@ public class RemoteException : Exception
7094 {
7195 Type = error . Type ;
7296 StackTrace = error . StackTrace ;
97+ if ( error . Data != null )
98+ {
99+ foreach ( var key in error . Data )
100+ {
101+ var value = key . Value ;
102+ Data [ key . Key ] = value ;
103+ }
104+ }
73105 }
74106 public string Type { get ; }
75107 public override string StackTrace { get ; }
0 commit comments