blob: 918708786c3056c9514b7b2bc2066f0d49a88ad4 [file] [log] [blame]
Mark Slee2ac60ed2007-09-19 21:10:18 +00001#import "TApplicationException.h"
2#import "TProtocolUtil.h"
3
4@implementation TApplicationException
5
6- (id) initWithType: (int) type
7 reason: (NSString *) reason
8{
Mark Slee84406052007-11-20 01:39:25 +00009 mType = type;
10
Mark Slee2ac60ed2007-09-19 21:10:18 +000011 NSString * name;
12 switch (type) {
13 case TApplicationException_UNKNOWN_METHOD:
14 name = @"Unknown method";
15 break;
16 case TApplicationException_INVALID_MESSAGE_TYPE:
17 name = @"Invalid message type";
18 break;
19 case TApplicationException_WRONG_METHOD_NAME:
20 name = @"Wrong method name";
21 break;
22 case TApplicationException_BAD_SEQUENCE_ID:
23 name = @"Bad sequence ID";
24 break;
25 case TApplicationException_MISSING_RESULT:
26 name = @"Missing result";
27 break;
28 default:
29 name = @"Unknown";
30 break;
31 }
32
33 self = [super initWithName: name reason: reason userInfo: nil];
34 return self;
35}
36
37
38+ (TApplicationException *) read: (id <TProtocol>) protocol
39{
40 NSString * reason = nil;
41 int type = TApplicationException_UNKNOWN;
42 int fieldType;
43 int fieldID;
44
Mark Slee84406052007-11-20 01:39:25 +000045 [protocol readStructBeginReturningName: NULL];
46
Mark Slee2ac60ed2007-09-19 21:10:18 +000047 while (true) {
48 [protocol readFieldBeginReturningName: NULL
49 type: &fieldType
50 fieldID: &fieldID];
David Reiss0c90f6f2008-02-06 22:18:40 +000051 if (fieldType == TType_STOP) {
Mark Slee2ac60ed2007-09-19 21:10:18 +000052 break;
53 }
54 switch (fieldID) {
55 case 1:
56 if (fieldType == TType_STRING) {
57 reason = [protocol readString];
David Reiss0c90f6f2008-02-06 22:18:40 +000058 } else {
Mark Slee2ac60ed2007-09-19 21:10:18 +000059 [TProtocolUtil skipType: fieldType onProtocol: protocol];
60 }
61 break;
62 case 2:
63 if (fieldType == TType_I32) {
64 type = [protocol readI32];
David Reiss0c90f6f2008-02-06 22:18:40 +000065 } else {
Mark Slee2ac60ed2007-09-19 21:10:18 +000066 [TProtocolUtil skipType: fieldType onProtocol: protocol];
67 }
68 break;
69 default:
70 [TProtocolUtil skipType: fieldType onProtocol: protocol];
71 break;
72 }
73 [protocol readFieldEnd];
74 }
75 [protocol readStructEnd];
76
77 return [TApplicationException exceptionWithType: type reason: reason];
78}
79
80
Mark Slee84406052007-11-20 01:39:25 +000081- (void) write: (id <TProtocol>) protocol
82{
83 [protocol writeStructBeginWithName: @"TApplicationException"];
84
85 if ([self reason] != nil) {
86 [protocol writeFieldBeginWithName: @"message"
87 type: TType_STRING
88 fieldID: 1];
89 [protocol writeString: [self reason]];
90 [protocol writeFieldEnd];
91 }
92
93 [protocol writeFieldBeginWithName: @"type"
94 type: TType_I32
95 fieldID: 2];
96 [protocol writeI32: mType];
97 [protocol writeFieldEnd];
98
99 [protocol writeFieldStop];
100 [protocol writeStructEnd];
101}
102
103
Mark Slee2ac60ed2007-09-19 21:10:18 +0000104+ (TApplicationException *) exceptionWithType: (int) type
105 reason: (NSString *) reason
106{
107 return [[[TApplicationException alloc] initWithType: type
108 reason: reason] autorelease];
109}
110
111@end