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