RpcException.cs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. /// </summary>
  37. /// <param name="status">Resulting status of a call.</param>
  38. /// <param name="message">The exception message.</param>
  39. public RpcException(Status status, string message) : this(status, Metadata.Empty, message)
  40. {
  41. }
  42. /// <summary>
  43. /// Creates a new <c>RpcException</c> associated with given status and trailing response metadata.
  44. /// </summary>
  45. /// <param name="status">Resulting status of a call.</param>
  46. /// <param name="trailers">Response trailing metadata.</param>
  47. public RpcException(Status status, Metadata trailers) : this(status, trailers, status.ToString())
  48. {
  49. }
  50. /// <summary>
  51. /// Creates a new <c>RpcException</c> associated with given status, message and trailing response metadata.
  52. /// </summary>
  53. /// <param name="status">Resulting status of a call.</param>
  54. /// <param name="trailers">Response trailing metadata.</param>
  55. /// <param name="message">The exception message.</param>
  56. public RpcException(Status status, Metadata trailers, string message) : base(message)
  57. {
  58. this.status = status;
  59. this.trailers = GrpcPreconditions.CheckNotNull(trailers);
  60. }
  61. /// <summary>
  62. /// Resulting status of the call.
  63. /// </summary>
  64. public Status Status
  65. {
  66. get
  67. {
  68. return status;
  69. }
  70. }
  71. /// <summary>
  72. /// Returns the status code of the call, as a convenient alternative to <see cref="StatusCode">Status.StatusCode</see>.
  73. /// </summary>
  74. public StatusCode StatusCode
  75. {
  76. get
  77. {
  78. return status.StatusCode;
  79. }
  80. }
  81. /// <summary>
  82. /// Gets the call trailing metadata.
  83. /// 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).
  84. /// Instances of <c>RpcException</c> thrown by the server-side part of the stack will have trailers always set to empty.
  85. /// </summary>
  86. public Metadata Trailers
  87. {
  88. get
  89. {
  90. return trailers;
  91. }
  92. }
  93. }
  94. }