RpcException.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using Grpc.Core.Utils;
  18. namespace Grpc.Core
  19. {
  20. /// <summary>
  21. /// Thrown when remote procedure call fails. Every <c>RpcException</c> is associated with a resulting <see cref="Status"/> of the call.
  22. /// </summary>
  23. public class RpcException : Exception
  24. {
  25. private readonly Status status;
  26. private readonly Metadata trailers;
  27. /// <summary>
  28. /// Creates a new <c>RpcException</c> associated with given status.
  29. /// </summary>
  30. /// <param name="status">Resulting status of a call.</param>
  31. public RpcException(Status status) : this(status, Metadata.Empty, status.ToString())
  32. {
  33. }
  34. /// <summary>
  35. /// Creates a new <c>RpcException</c> associated with given status and message.
  36. /// NOTE: the exception message is not sent to the remote peer. Use <c>status.Details</c> to pass error
  37. /// details to the peer.
  38. /// </summary>
  39. /// <param name="status">Resulting status of a call.</param>
  40. /// <param name="message">The exception message.</param>
  41. public RpcException(Status status, string message) : this(status, Metadata.Empty, message)
  42. {
  43. }
  44. /// <summary>
  45. /// Creates a new <c>RpcException</c> associated with given status and trailing response metadata.
  46. /// </summary>
  47. /// <param name="status">Resulting status of a call.</param>
  48. /// <param name="trailers">Response trailing metadata.</param>
  49. public RpcException(Status status, Metadata trailers) : this(status, trailers, status.ToString())
  50. {
  51. }
  52. /// <summary>
  53. /// Creates a new <c>RpcException</c> associated with given status, message and trailing response metadata.
  54. /// NOTE: the exception message is not sent to the remote peer. Use <c>status.Details</c> to pass error
  55. /// details to the peer.
  56. /// </summary>
  57. /// <param name="status">Resulting status of a call.</param>
  58. /// <param name="trailers">Response trailing metadata.</param>
  59. /// <param name="message">The exception message.</param>
  60. public RpcException(Status status, Metadata trailers, string message) : base(message)
  61. {
  62. this.status = status;
  63. this.trailers = GrpcPreconditions.CheckNotNull(trailers);
  64. }
  65. /// <summary>
  66. /// Resulting status of the call.
  67. /// </summary>
  68. public Status Status
  69. {
  70. get
  71. {
  72. return status;
  73. }
  74. }
  75. /// <summary>
  76. /// Returns the status code of the call, as a convenient alternative to <see cref="StatusCode">Status.StatusCode</see>.
  77. /// </summary>
  78. public StatusCode StatusCode
  79. {
  80. get
  81. {
  82. return status.StatusCode;
  83. }
  84. }
  85. /// <summary>
  86. /// Gets the call trailing metadata.
  87. /// Trailers only have meaningful content for client-side calls (in which case they represent the trailing metadata sent by the server when closing the call).
  88. /// Instances of <c>RpcException</c> thrown by the server-side part of the stack will have trailers always set to empty.
  89. /// </summary>
  90. public Metadata Trailers
  91. {
  92. get
  93. {
  94. return trailers;
  95. }
  96. }
  97. }
  98. }