RpcExceptions.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. using System;
  2. namespace EdjCase.JsonRpc.Core
  3. {
  4. /// <summary>
  5. /// Rpc server exception that contains Rpc specfic error info
  6. /// </summary>
  7. public class RpcException : Exception
  8. {
  9. /// <summary>
  10. /// Rpc error code that corresponds to the documented integer codes
  11. /// </summary>
  12. public int ErrorCode { get; }
  13. /// <summary>
  14. /// Custom data attached to the error if needed
  15. /// </summary>
  16. public object RpcData { get; }
  17. /// <param name="errorCode">Rpc error code</param>
  18. /// <param name="message">Error message</param>
  19. /// <param name="data">Custom data if needed for error response</param>
  20. /// <param name="innerException">Inner exception (optional)</param>
  21. public RpcException(int errorCode, string message, Exception innerException = null, object data = null)
  22. : base(message, innerException)
  23. {
  24. this.ErrorCode = errorCode;
  25. this.RpcData = data;
  26. }
  27. /// <param name="errorCode">Rpc error code</param>
  28. /// <param name="message">Error message</param>
  29. /// <param name="data">Custom data if needed for error response</param>
  30. /// <param name="innerException">Inner exception (optional)</param>
  31. public RpcException(RpcErrorCode errorCode, string message, Exception innerException = null, object data = null)
  32. : this((int)errorCode, message, innerException, data)
  33. {
  34. }
  35. public RpcError ToRpcError(bool includeServerErrors)
  36. {
  37. string message = this.Message;
  38. if (includeServerErrors)
  39. {
  40. message += Environment.NewLine + "Exception: " + this.InnerException;
  41. }
  42. return new RpcError(this.ErrorCode, this.Message, this.RpcData);
  43. }
  44. }
  45. }