Kevin Clark | ab4460d | 2009-03-20 02:28:41 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. 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, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 19 | |
| 20 | using System; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 21 | using Thrift.Protocol; |
| 22 | |
| 23 | namespace Thrift |
| 24 | { |
| 25 | public class TApplicationException : Exception |
| 26 | { |
| 27 | protected ExceptionType type; |
| 28 | |
| 29 | public TApplicationException() |
| 30 | { |
| 31 | } |
| 32 | |
| 33 | public TApplicationException(ExceptionType type) |
| 34 | { |
| 35 | this.type = type; |
| 36 | } |
| 37 | |
| 38 | public TApplicationException(ExceptionType type, string message) |
| 39 | : base(message) |
| 40 | { |
| 41 | this.type = type; |
| 42 | } |
| 43 | |
| 44 | public static TApplicationException Read(TProtocol iprot) |
| 45 | { |
| 46 | TField field; |
David Reiss | 7f42bcf | 2008-01-11 20:59:12 +0000 | [diff] [blame] | 47 | |
| 48 | string message = null; |
| 49 | ExceptionType type = ExceptionType.Unknown; |
| 50 | |
| 51 | while (true) |
| 52 | { |
| 53 | field = iprot.ReadFieldBegin(); |
| 54 | if (field.Type == TType.Stop) |
| 55 | { |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | switch (field.ID) |
| 60 | { |
| 61 | case 1: |
| 62 | if (field.Type == TType.String) |
| 63 | { |
| 64 | message = iprot.ReadString(); |
| 65 | } |
| 66 | else |
| 67 | { |
| 68 | TProtocolUtil.Skip(iprot, field.Type); |
| 69 | } |
| 70 | break; |
| 71 | case 2: |
| 72 | if (field.Type == TType.I32) |
| 73 | { |
| 74 | type = (ExceptionType)iprot.ReadI32(); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | TProtocolUtil.Skip(iprot, field.Type); |
| 79 | } |
| 80 | break; |
| 81 | default: |
| 82 | TProtocolUtil.Skip(iprot, field.Type); |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | iprot.ReadFieldEnd(); |
| 87 | } |
| 88 | |
| 89 | iprot.ReadStructEnd(); |
| 90 | |
| 91 | return new TApplicationException(type, message); |
| 92 | } |
| 93 | |
| 94 | public void Write(TProtocol oprot) |
| 95 | { |
| 96 | TStruct struc = new TStruct("TApplicationException"); |
| 97 | TField field = new TField(); |
| 98 | |
| 99 | oprot.WriteStructBegin(struc); |
| 100 | |
| 101 | if (!String.IsNullOrEmpty(Message)) |
| 102 | { |
| 103 | field.Name = "message"; |
| 104 | field.Type = TType.String; |
| 105 | field.ID = 1; |
| 106 | oprot.WriteFieldBegin(field); |
| 107 | oprot.WriteString(Message); |
| 108 | oprot.WriteFieldEnd(); |
| 109 | } |
| 110 | |
| 111 | field.Name = "type"; |
| 112 | field.Type = TType.I32; |
| 113 | field.ID = 2; |
| 114 | oprot.WriteFieldBegin(field); |
| 115 | oprot.WriteI32((int)type); |
| 116 | oprot.WriteFieldEnd(); |
| 117 | oprot.WriteFieldStop(); |
| 118 | oprot.WriteStructEnd(); |
| 119 | } |
| 120 | |
| 121 | public enum ExceptionType |
| 122 | { |
| 123 | Unknown, |
| 124 | UnknownMethod, |
| 125 | InvalidMessageType, |
| 126 | WrongMethodName, |
| 127 | BadSequenceID, |
| 128 | MissingResult |
| 129 | } |
| 130 | } |
| 131 | } |