RpcException.cs 3.3 KB

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