blob: 1f658de6d478f3340e7e3e6a9a0c10a70cd0e89a [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{
9 NSString * name;
10 switch (type) {
11 case TApplicationException_UNKNOWN_METHOD:
12 name = @"Unknown method";
13 break;
14 case TApplicationException_INVALID_MESSAGE_TYPE:
15 name = @"Invalid message type";
16 break;
17 case TApplicationException_WRONG_METHOD_NAME:
18 name = @"Wrong method name";
19 break;
20 case TApplicationException_BAD_SEQUENCE_ID:
21 name = @"Bad sequence ID";
22 break;
23 case TApplicationException_MISSING_RESULT:
24 name = @"Missing result";
25 break;
26 default:
27 name = @"Unknown";
28 break;
29 }
30
31 self = [super initWithName: name reason: reason userInfo: nil];
32 return self;
33}
34
35
36+ (TApplicationException *) read: (id <TProtocol>) protocol
37{
38 NSString * reason = nil;
39 int type = TApplicationException_UNKNOWN;
40 int fieldType;
41 int fieldID;
42
43 while (true) {
44 [protocol readFieldBeginReturningName: NULL
45 type: &fieldType
46 fieldID: &fieldID];
47 if (fieldType == TType_STOP) {
48 break;
49 }
50 switch (fieldID) {
51 case 1:
52 if (fieldType == TType_STRING) {
53 reason = [protocol readString];
54 } else {
55 [TProtocolUtil skipType: fieldType onProtocol: protocol];
56 }
57 break;
58 case 2:
59 if (fieldType == TType_I32) {
60 type = [protocol readI32];
61 } else {
62 [TProtocolUtil skipType: fieldType onProtocol: protocol];
63 }
64 break;
65 default:
66 [TProtocolUtil skipType: fieldType onProtocol: protocol];
67 break;
68 }
69 [protocol readFieldEnd];
70 }
71 [protocol readStructEnd];
72
73 return [TApplicationException exceptionWithType: type reason: reason];
74}
75
76
77+ (TApplicationException *) exceptionWithType: (int) type
78 reason: (NSString *) reason
79{
80 return [[[TApplicationException alloc] initWithType: type
81 reason: reason] autorelease];
82}
83
84@end