blob: 974dfc5063627fd7b8b6d9ca88595ab996dd49c4 [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;
Roger Meier01931492012-12-22 21:31:03 +010048 case TApplicationException_INTERNAL_ERROR:
49 name = @"Internal error";
50 break;
51 case TApplicationException_PROTOCOL_ERROR:
52 name = @"Protocol error";
53 break;
54 case TApplicationException_INVALID_TRANSFORM:
55 name = @"Invalid transform";
56 break;
57 case TApplicationException_INVALID_PROTOCOL:
58 name = @"Invalid protocol";
59 break;
60 case TApplicationException_UNSUPPORTED_CLIENT_TYPE:
61 name = @"Unsupported client type";
62 break;
Mark Slee2ac60ed2007-09-19 21:10:18 +000063 default:
64 name = @"Unknown";
65 break;
66 }
67
68 self = [super initWithName: name reason: reason userInfo: nil];
69 return self;
70}
71
72
73+ (TApplicationException *) read: (id <TProtocol>) protocol
74{
75 NSString * reason = nil;
76 int type = TApplicationException_UNKNOWN;
77 int fieldType;
78 int fieldID;
79
Mark Slee84406052007-11-20 01:39:25 +000080 [protocol readStructBeginReturningName: NULL];
81
Mark Slee2ac60ed2007-09-19 21:10:18 +000082 while (true) {
83 [protocol readFieldBeginReturningName: NULL
84 type: &fieldType
85 fieldID: &fieldID];
David Reiss0c90f6f2008-02-06 22:18:40 +000086 if (fieldType == TType_STOP) {
Mark Slee2ac60ed2007-09-19 21:10:18 +000087 break;
88 }
89 switch (fieldID) {
90 case 1:
91 if (fieldType == TType_STRING) {
92 reason = [protocol readString];
David Reiss0c90f6f2008-02-06 22:18:40 +000093 } else {
Mark Slee2ac60ed2007-09-19 21:10:18 +000094 [TProtocolUtil skipType: fieldType onProtocol: protocol];
95 }
96 break;
97 case 2:
98 if (fieldType == TType_I32) {
99 type = [protocol readI32];
David Reiss0c90f6f2008-02-06 22:18:40 +0000100 } else {
Mark Slee2ac60ed2007-09-19 21:10:18 +0000101 [TProtocolUtil skipType: fieldType onProtocol: protocol];
102 }
103 break;
104 default:
105 [TProtocolUtil skipType: fieldType onProtocol: protocol];
106 break;
107 }
108 [protocol readFieldEnd];
109 }
110 [protocol readStructEnd];
111
112 return [TApplicationException exceptionWithType: type reason: reason];
113}
114
115
Mark Slee84406052007-11-20 01:39:25 +0000116- (void) write: (id <TProtocol>) protocol
117{
118 [protocol writeStructBeginWithName: @"TApplicationException"];
119
120 if ([self reason] != nil) {
121 [protocol writeFieldBeginWithName: @"message"
122 type: TType_STRING
123 fieldID: 1];
124 [protocol writeString: [self reason]];
125 [protocol writeFieldEnd];
126 }
127
128 [protocol writeFieldBeginWithName: @"type"
129 type: TType_I32
130 fieldID: 2];
131 [protocol writeI32: mType];
132 [protocol writeFieldEnd];
133
134 [protocol writeFieldStop];
135 [protocol writeStructEnd];
136}
137
138
Mark Slee2ac60ed2007-09-19 21:10:18 +0000139+ (TApplicationException *) exceptionWithType: (int) type
140 reason: (NSString *) reason
141{
142 return [[[TApplicationException alloc] initWithType: type
Jake Farrell9689d892011-12-06 01:07:17 +0000143 reason: reason] autorelease_stub];
Mark Slee2ac60ed2007-09-19 21:10:18 +0000144}
145
146@end