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 /// NOTE: the exception message is not sent to the remote peer. Use <c>status.Details</c> to pass error 43 /// details to the peer. 44 /// </summary> 45 /// <param name="status">Resulting status of a call.</param> 46 /// <param name="message">The exception message.</param> RpcException(Status status, string message)47 public RpcException(Status status, string message) : this(status, Metadata.Empty, message) 48 { 49 } 50 51 /// <summary> 52 /// Creates a new <c>RpcException</c> associated with given status and trailing response metadata. 53 /// </summary> 54 /// <param name="status">Resulting status of a call.</param> 55 /// <param name="trailers">Response trailing metadata.</param> RpcException(Status status, Metadata trailers)56 public RpcException(Status status, Metadata trailers) : this(status, trailers, status.ToString()) 57 { 58 } 59 60 /// <summary> 61 /// Creates a new <c>RpcException</c> associated with given status, message and trailing response metadata. 62 /// NOTE: the exception message is not sent to the remote peer. Use <c>status.Details</c> to pass error 63 /// details to the peer. 64 /// </summary> 65 /// <param name="status">Resulting status of a call.</param> 66 /// <param name="trailers">Response trailing metadata.</param> 67 /// <param name="message">The exception message.</param> RpcException(Status status, Metadata trailers, string message)68 public RpcException(Status status, Metadata trailers, string message) : base(message) 69 { 70 this.status = status; 71 this.trailers = GrpcPreconditions.CheckNotNull(trailers); 72 } 73 74 /// <summary> 75 /// Resulting status of the call. 76 /// </summary> 77 public Status Status 78 { 79 get 80 { 81 return status; 82 } 83 } 84 85 /// <summary> 86 /// Returns the status code of the call, as a convenient alternative to <see cref="StatusCode">Status.StatusCode</see>. 87 /// </summary> 88 public StatusCode StatusCode 89 { 90 get 91 { 92 return status.StatusCode; 93 } 94 } 95 96 /// <summary> 97 /// Gets the call trailing metadata. 98 /// 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). 99 /// Instances of <c>RpcException</c> thrown by the server-side part of the stack will have trailers always set to empty. 100 /// </summary> 101 public Metadata Trailers 102 { 103 get 104 { 105 return trailers; 106 } 107 } 108 } 109 } 110