/// <summary> /// Initializes a new <see cref="ClrExceptionErrorData"/> from an existing exception. /// </summary> /// <param name="ex">The exception from which to extract error data.</param> /// <param name="includesStackTrace">Whether to set <see cref="StackTrace"/> from the given exception. Note that the stack trace may contain sensitive information.</param> /// <exception cref="ArgumentNullException"><paramref name="ex"/> is <c>null</c>.</exception> public static ClrExceptionErrorData FromException(Exception ex, bool includesStackTrace) { if (ex == null) { throw new ArgumentNullException(nameof(ex)); } var inst = new ClrExceptionErrorData { ExceptionType = ex.GetType().FullName, Message = ex.Message, Data = ex.Data, HResult = ex.HResult, HelpLink = ex.HelpLink, StackTrace = ex.StackTrace, InnerException = ex.InnerException == null ? null : FromException(ex.InnerException, includesStackTrace) }; // Consider extract such special behaviors into a interface, // and let Exception-implementor implement it. if (ex is JsonRpcRemoteException re && re.InnerException == null && re.RemoteException != null) { // For JsonRpcRemoteException, we treat RemoteException as InnerException, // if possible. This can be helpful to maintain as much information as we can, // especially when we are relaying JSON RPC operations through the channels. inst.InnerException = re.RemoteException; } return(inst); }
/// <summary> /// Instantiates a new <see cref="ResponseError"/> from an existing <see cref="ClrExceptionErrorData"/>. /// </summary> /// <param name="errorData">The error information.</param> /// <exception cref="ArgumentNullException"><paramref name="errorData"/> is <c>null</c>.</exception> public static ResponseError FromException(ClrExceptionErrorData errorData) { if (errorData == null) { throw new ArgumentNullException(nameof(errorData)); } return(new ResponseError(JsonRpcErrorCode.UnhandledClrException, $"{errorData.ExceptionType}: {errorData.Message}", errorData)); }
/// <summary> /// Instantiates a new <see cref="ResponseError"/> from an existing <see cref="Exception"/>. /// </summary> /// <param name="ex">The exception containing the error information.</param> /// <param name="includesStackTrace">Whether to include <see cref="Exception.StackTrace"/> from the given exception. Note that the stack trace may contain sensitive information.</param> /// <exception cref="ArgumentNullException"><paramref name="ex"/> is <c>null</c>.</exception> public static ResponseError FromException(Exception ex, bool includesStackTrace) { if (ex == null) { throw new ArgumentNullException(nameof(ex)); } if (ex is JsonRpcException re && re.Error != null) { return(re.Error); } return(FromException(ClrExceptionErrorData.FromException(ex, includesStackTrace))); }