| 
 | 1 | +// Copyright The OpenTelemetry Authors  | 
 | 2 | +// SPDX-License-Identifier: Apache-2.0  | 
 | 3 | + | 
 | 4 | +// Copyright 2015 gRPC authors.  | 
 | 5 | +//  | 
 | 6 | +// Licensed under the Apache License, Version 2.0 (the "License");  | 
 | 7 | +// you may not use this file except in compliance with the License.  | 
 | 8 | +// You may obtain a copy of the License at  | 
 | 9 | +//  | 
 | 10 | +//     http://www.apache.org/licenses/LICENSE-2.0  | 
 | 11 | +//  | 
 | 12 | +// Unless required by applicable law or agreed to in writing, software  | 
 | 13 | +// distributed under the License is distributed on an "AS IS" BASIS,  | 
 | 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  | 
 | 15 | +// See the License for the specific language governing permissions and  | 
 | 16 | +// limitations under the License.  | 
 | 17 | + | 
 | 18 | +using System.Diagnostics;  | 
 | 19 | + | 
 | 20 | +namespace OpenTelemetry.Exporter.OpenTelemetryProtocol.Implementation.ExportClient.Grpc;  | 
 | 21 | + | 
 | 22 | +/// <summary>  | 
 | 23 | +/// Represents RPC result, which consists of <see cref="StatusCode"/> and an optional detail string.  | 
 | 24 | +/// </summary>  | 
 | 25 | +[DebuggerDisplay("{DebuggerToString(),nq}")]  | 
 | 26 | +internal struct Status  | 
 | 27 | +{  | 
 | 28 | +    /// <summary>  | 
 | 29 | +    /// Default result of a successful RPC. StatusCode=OK, empty details message.  | 
 | 30 | +    /// </summary>  | 
 | 31 | +    public static readonly Status DefaultSuccess = new Status(StatusCode.OK, string.Empty);  | 
 | 32 | + | 
 | 33 | +    /// <summary>  | 
 | 34 | +    /// Default result of a cancelled RPC. StatusCode=Cancelled, empty details message.  | 
 | 35 | +    /// </summary>  | 
 | 36 | +    public static readonly Status DefaultCancelled = new Status(StatusCode.Cancelled, string.Empty);  | 
 | 37 | + | 
 | 38 | +    /// <summary>  | 
 | 39 | +    /// Initializes a new instance of the <see cref="Status"/> struct.  | 
 | 40 | +    /// </summary>  | 
 | 41 | +    /// <param name="statusCode">Status code.</param>  | 
 | 42 | +    /// <param name="detail">Detail.</param>  | 
 | 43 | +    public Status(StatusCode statusCode, string detail)  | 
 | 44 | +        : this(statusCode, detail, null)  | 
 | 45 | +    {  | 
 | 46 | +    }  | 
 | 47 | + | 
 | 48 | +    /// <summary>  | 
 | 49 | +    /// Initializes a new instance of the <see cref="Status"/> struct.  | 
 | 50 | +    /// Users should not use this constructor, except for creating instances for testing.  | 
 | 51 | +    /// The debug error string should only be populated by gRPC internals.  | 
 | 52 | +    /// Note: experimental API that can change or be removed without any prior notice.  | 
 | 53 | +    /// </summary>  | 
 | 54 | +    /// <param name="statusCode">Status code.</param>  | 
 | 55 | +    /// <param name="detail">Detail.</param>  | 
 | 56 | +    /// <param name="debugException">Optional internal error details.</param>  | 
 | 57 | +    public Status(StatusCode statusCode, string detail, Exception? debugException)  | 
 | 58 | +    {  | 
 | 59 | +        this.StatusCode = statusCode;  | 
 | 60 | +        this.Detail = detail;  | 
 | 61 | +        this.DebugException = debugException;  | 
 | 62 | +    }  | 
 | 63 | + | 
 | 64 | +    /// <summary>  | 
 | 65 | +    /// Gets the gRPC status code. OK indicates success, all other values indicate an error.  | 
 | 66 | +    /// </summary>  | 
 | 67 | +    public StatusCode StatusCode { get; }  | 
 | 68 | + | 
 | 69 | +    /// <summary>  | 
 | 70 | +    /// Gets the detail.  | 
 | 71 | +    /// </summary>  | 
 | 72 | +    public string Detail { get; }  | 
 | 73 | + | 
 | 74 | +    /// <summary>  | 
 | 75 | +    /// Gets in case of an error, this field may contain additional error details to help with debugging.  | 
 | 76 | +    /// This field will be only populated on a client and its value is generated locally,  | 
 | 77 | +    /// based on the internal state of the gRPC client stack (i.e. the value is never sent over the wire).  | 
 | 78 | +    /// Note that this field is available only for debugging purposes, the application logic should  | 
 | 79 | +    /// never rely on values of this field (it should use <c>StatusCode</c> and <c>Detail</c> instead).  | 
 | 80 | +    /// Example: when a client fails to connect to a server, this field may provide additional details  | 
 | 81 | +    /// why the connection to the server has failed.  | 
 | 82 | +    /// Note: experimental API that can change or be removed without any prior notice.  | 
 | 83 | +    /// </summary>  | 
 | 84 | +    public Exception? DebugException { get; }  | 
 | 85 | + | 
 | 86 | +    public override string ToString()  | 
 | 87 | +    {  | 
 | 88 | +        if (this.DebugException != null)  | 
 | 89 | +        {  | 
 | 90 | +            return $"Status(StatusCode=\"{this.StatusCode}\", Detail=\"{this.Detail}\"," +  | 
 | 91 | +                $" DebugException=\"{this.DebugException.GetType()}: {this.DebugException.Message}\")";  | 
 | 92 | +        }  | 
 | 93 | + | 
 | 94 | +        return $"Status(StatusCode=\"{this.StatusCode}\", Detail=\"{this.Detail}\")";  | 
 | 95 | +    }  | 
 | 96 | + | 
 | 97 | +    private string DebuggerToString()  | 
 | 98 | +    {  | 
 | 99 | +        var text = $"StatusCode = {this.StatusCode}";  | 
 | 100 | +        if (!string.IsNullOrEmpty(this.Detail))  | 
 | 101 | +        {  | 
 | 102 | +            text += $@", Detail = ""{this.Detail}""";  | 
 | 103 | +        }  | 
 | 104 | + | 
 | 105 | +        if (this.DebugException != null)  | 
 | 106 | +        {  | 
 | 107 | +            text += $@", DebugException = ""{this.DebugException.GetType()}: {this.DebugException.Message}""";  | 
 | 108 | +        }  | 
 | 109 | + | 
 | 110 | +        return text;  | 
 | 111 | +    }  | 
 | 112 | +}  | 
0 commit comments