]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/thrift/lib/netcore/Thrift/TApplicationException.cs
buildsys: switch source download to quincy
[ceph.git] / ceph / src / jaegertracing / thrift / lib / netcore / Thrift / TApplicationException.cs
1 // Licensed to the Apache Software Foundation(ASF) under one
2 // or more contributor license agreements.See the NOTICE file
3 // distributed with this work for additional information
4 // regarding copyright ownership.The ASF licenses this file
5 // to you under the Apache License, Version 2.0 (the
6 // "License"); you may not use this file except in compliance
7 // with the License. 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,
12 // software distributed under the License is distributed on an
13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, either express or implied. See the License for the
15 // specific language governing permissions and limitations
16 // under the License.
17
18 using System.Threading;
19 using System.Threading.Tasks;
20 using Thrift.Protocols;
21 using Thrift.Protocols.Entities;
22 using Thrift.Protocols.Utilities;
23
24 namespace Thrift
25 {
26 // ReSharper disable once InconsistentNaming
27 public class TApplicationException : TException
28 {
29 public enum ExceptionType
30 {
31 Unknown,
32 UnknownMethod,
33 InvalidMessageType,
34 WrongMethodName,
35 BadSequenceId,
36 MissingResult,
37 InternalError,
38 ProtocolError,
39 InvalidTransform,
40 InvalidProtocol,
41 UnsupportedClientType
42 }
43
44 private const int MessageTypeFieldId = 1;
45 private const int ExTypeFieldId = 2;
46
47 protected ExceptionType Type;
48
49 public TApplicationException()
50 {
51 }
52
53 public TApplicationException(ExceptionType type)
54 {
55 Type = type;
56 }
57
58 public TApplicationException(ExceptionType type, string message)
59 : base(message)
60 {
61 Type = type;
62 }
63
64 public static async Task<TApplicationException> ReadAsync(TProtocol inputProtocol, CancellationToken cancellationToken)
65 {
66 string message = null;
67 var type = ExceptionType.Unknown;
68
69 await inputProtocol.ReadStructBeginAsync(cancellationToken);
70 while (true)
71 {
72 var field = await inputProtocol.ReadFieldBeginAsync(cancellationToken);
73 if (field.Type == TType.Stop)
74 {
75 break;
76 }
77
78 switch (field.ID)
79 {
80 case MessageTypeFieldId:
81 if (field.Type == TType.String)
82 {
83 message = await inputProtocol.ReadStringAsync(cancellationToken);
84 }
85 else
86 {
87 await TProtocolUtil.SkipAsync(inputProtocol, field.Type, cancellationToken);
88 }
89 break;
90 case ExTypeFieldId:
91 if (field.Type == TType.I32)
92 {
93 type = (ExceptionType) await inputProtocol.ReadI32Async(cancellationToken);
94 }
95 else
96 {
97 await TProtocolUtil.SkipAsync(inputProtocol, field.Type, cancellationToken);
98 }
99 break;
100 default:
101 await TProtocolUtil.SkipAsync(inputProtocol, field.Type, cancellationToken);
102 break;
103 }
104
105 await inputProtocol.ReadFieldEndAsync(cancellationToken);
106 }
107
108 await inputProtocol.ReadStructEndAsync(cancellationToken);
109
110 return new TApplicationException(type, message);
111 }
112
113 public async Task WriteAsync(TProtocol outputProtocol, CancellationToken cancellationToken)
114 {
115 if (cancellationToken.IsCancellationRequested)
116 {
117 await Task.FromCanceled(cancellationToken);
118 }
119
120 const string messageTypeFieldName = "message";
121 const string exTypeFieldName = "exType";
122 const string structApplicationExceptionName = "TApplicationException";
123
124 var struc = new TStruct(structApplicationExceptionName);
125 var field = new TField();
126
127 await outputProtocol.WriteStructBeginAsync(struc, cancellationToken);
128
129 if (!string.IsNullOrEmpty(Message))
130 {
131 field.Name = messageTypeFieldName;
132 field.Type = TType.String;
133 field.ID = MessageTypeFieldId;
134 await outputProtocol.WriteFieldBeginAsync(field, cancellationToken);
135 await outputProtocol.WriteStringAsync(Message, cancellationToken);
136 await outputProtocol.WriteFieldEndAsync(cancellationToken);
137 }
138
139 field.Name = exTypeFieldName;
140 field.Type = TType.I32;
141 field.ID = ExTypeFieldId;
142
143 await outputProtocol.WriteFieldBeginAsync(field, cancellationToken);
144 await outputProtocol.WriteI32Async((int) Type, cancellationToken);
145 await outputProtocol.WriteFieldEndAsync(cancellationToken);
146 await outputProtocol.WriteFieldStopAsync(cancellationToken);
147 await outputProtocol.WriteStructEndAsync(cancellationToken);
148 }
149 }
150 }