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 
17 using System;
18 
19 namespace Grpc.Core
20 {
21     /// <summary>
22     /// Represents RPC result, which consists of <see cref="StatusCode"/> and an optional detail string.
23     /// </summary>
24     public struct Status
25     {
26         /// <summary>
27         /// Default result of a successful RPC. StatusCode=OK, empty details message.
28         /// </summary>
29         public static readonly Status DefaultSuccess = new Status(StatusCode.OK, "");
30 
31         /// <summary>
32         /// Default result of a cancelled RPC. StatusCode=Cancelled, empty details message.
33         /// </summary>
34         public static readonly Status DefaultCancelled = new Status(StatusCode.Cancelled, "");
35 
36         /// <summary>
37         /// Creates a new instance of <c>Status</c>.
38         /// </summary>
39         /// <param name="statusCode">Status code.</param>
40         /// <param name="detail">Detail.</param>
StatusGrpc.Core.Status41         public Status(StatusCode statusCode, string detail) : this(statusCode, detail, null)
42         {
43         }
44 
45         /// <summary>
46         /// Creates a new instance of <c>Status</c>.
47         /// Users should not use this constructor, except for creating instances for testing.
48         /// The debug error string should only be populated by gRPC internals.
49         /// Note: experimental API that can change or be removed without any prior notice.
50         /// </summary>
51         /// <param name="statusCode">Status code.</param>
52         /// <param name="detail">Detail.</param>
53         /// <param name="debugException">Optional internal error details.</param>
StatusGrpc.Core.Status54         public Status(StatusCode statusCode, string detail, Exception debugException)
55         {
56             StatusCode = statusCode;
57             Detail = detail;
58             DebugException = debugException;
59         }
60 
61         /// <summary>
62         /// Gets the gRPC status code. OK indicates success, all other values indicate an error.
63         /// </summary>
64         public StatusCode StatusCode { get; }
65 
66         /// <summary>
67         /// Gets the detail.
68         /// </summary>
69         public string Detail { get; }
70 
71         /// <summary>
72         /// In case of an error, this field may contain additional error details to help with debugging.
73         /// This field will be only populated on a client and its value is generated locally,
74         /// based on the internal state of the gRPC client stack (i.e. the value is never sent over the wire).
75         /// Note that this field is available only for debugging purposes, the application logic should
76         /// never rely on values of this field (it should use <c>StatusCode</c> and <c>Detail</c> instead).
77         /// Example: when a client fails to connect to a server, this field may provide additional details
78         /// why the connection to the server has failed.
79         /// Note: experimental API that can change or be removed without any prior notice.
80         /// </summary>
81         public Exception DebugException { get; }
82 
83         /// <summary>
84         /// Returns a <see cref="System.String"/> that represents the current <see cref="Grpc.Core.Status"/>.
85         /// </summary>
ToStringGrpc.Core.Status86         public override string ToString()
87         {
88             if (DebugException != null)
89             {
90                 return $"Status(StatusCode=\"{StatusCode}\", Detail=\"{Detail}\", DebugException=\"{DebugException}\")";
91             }
92             return $"Status(StatusCode=\"{StatusCode}\", Detail=\"{Detail}\")";
93         }
94     }
95 }
96