blob: 66c2f2b1b88d8f6a7e0576115141573bc416727c [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
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 */
19
Mark Slee2ac60ed2007-09-19 21:10:18 +000020#import "TApplicationException.h"
21#import "TProtocolUtil.h"
Jake Farrell9689d892011-12-06 01:07:17 +000022#import "TObjective-C.h"
Mark Slee2ac60ed2007-09-19 21:10:18 +000023
24@implementation TApplicationException
25
26- (id) initWithType: (int) type
27 reason: (NSString *) reason
28{
Mark Slee84406052007-11-20 01:39:25 +000029 mType = type;
30
Mark Slee2ac60ed2007-09-19 21:10:18 +000031 NSString * name;
32 switch (type) {
33 case TApplicationException_UNKNOWN_METHOD:
34 name = @"Unknown method";
35 break;
36 case TApplicationException_INVALID_MESSAGE_TYPE:
37 name = @"Invalid message type";
38 break;
39 case TApplicationException_WRONG_METHOD_NAME:
40 name = @"Wrong method name";
41 break;
42 case TApplicationException_BAD_SEQUENCE_ID:
43 name = @"Bad sequence ID";
44 break;
45 case TApplicationException_MISSING_RESULT:
46 name = @"Missing result";
47 break;
48 default:
49 name = @"Unknown";
50 break;
51 }
52
53 self = [super initWithName: name reason: reason userInfo: nil];
54 return self;
55}
56
57
58+ (TApplicationException *) read: (id <TProtocol>) protocol
59{
60 NSString * reason = nil;
61 int type = TApplicationException_UNKNOWN;
62 int fieldType;
63 int fieldID;
64
Mark Slee84406052007-11-20 01:39:25 +000065 [protocol readStructBeginReturningName: NULL];
66
Mark Slee2ac60ed2007-09-19 21:10:18 +000067 while (true) {
68 [protocol readFieldBeginReturningName: NULL
69 type: &fieldType
70 fieldID: &fieldID];
David Reiss0c90f6f2008-02-06 22:18:40 +000071 if (fieldType == TType_STOP) {
Mark Slee2ac60ed2007-09-19 21:10:18 +000072 break;
73 }
74 switch (fieldID) {
75 case 1:
76 if (fieldType == TType_STRING) {
77 reason = [protocol readString];
David Reiss0c90f6f2008-02-06 22:18:40 +000078 } else {
Mark Slee2ac60ed2007-09-19 21:10:18 +000079 [TProtocolUtil skipType: fieldType onProtocol: protocol];
80 }
81 break;
82 case 2:
83 if (fieldType == TType_I32) {
84 type = [protocol readI32];
David Reiss0c90f6f2008-02-06 22:18:40 +000085 } else {
Mark Slee2ac60ed2007-09-19 21:10:18 +000086 [TProtocolUtil skipType: fieldType onProtocol: protocol];
87 }
88 break;
89 default:
90 [TProtocolUtil skipType: fieldType onProtocol: protocol];
91 break;
92 }
93 [protocol readFieldEnd];
94 }
95 [protocol readStructEnd];
96
97 return [TApplicationException exceptionWithType: type reason: reason];
98}
99
100
Mark Slee84406052007-11-20 01:39:25 +0000101- (void) write: (id <TProtocol>) protocol
102{
103 [protocol writeStructBeginWithName: @"TApplicationException"];
104
105 if ([self reason] != nil) {
106 [protocol writeFieldBeginWithName: @"message"
107 type: TType_STRING
108 fieldID: 1];
109 [protocol writeString: [self reason]];
110 [protocol writeFieldEnd];
111 }
112
113 [protocol writeFieldBeginWithName: @"type"
114 type: TType_I32
115 fieldID: 2];
116 [protocol writeI32: mType];
117 [protocol writeFieldEnd];
118
119 [protocol writeFieldStop];
120 [protocol writeStructEnd];
121}
122
123
Mark Slee2ac60ed2007-09-19 21:10:18 +0000124+ (TApplicationException *) exceptionWithType: (int) type
125 reason: (NSString *) reason
126{
127 return [[[TApplicationException alloc] initWithType: type
Jake Farrell9689d892011-12-06 01:07:17 +0000128 reason: reason] autorelease_stub];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000129}
130
131@end