Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 1 | #import "TApplicationException.h" |
| 2 | #import "TProtocolUtil.h" |
| 3 | |
| 4 | @implementation TApplicationException |
| 5 | |
| 6 | - (id) initWithType: (int) type |
| 7 | reason: (NSString *) reason |
| 8 | { |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 9 | mType = type; |
| 10 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 11 | 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 Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 45 | [protocol readStructBeginReturningName: NULL]; |
| 46 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 47 | while (true) { |
| 48 | [protocol readFieldBeginReturningName: NULL |
| 49 | type: &fieldType |
| 50 | fieldID: &fieldID]; |
| 51 | if (fieldType == TType_STOP) { |
| 52 | break; |
| 53 | } |
| 54 | switch (fieldID) { |
| 55 | case 1: |
| 56 | if (fieldType == TType_STRING) { |
| 57 | reason = [protocol readString]; |
| 58 | } else { |
| 59 | [TProtocolUtil skipType: fieldType onProtocol: protocol]; |
| 60 | } |
| 61 | break; |
| 62 | case 2: |
| 63 | if (fieldType == TType_I32) { |
| 64 | type = [protocol readI32]; |
| 65 | } else { |
| 66 | [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 Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 81 | - (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 Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 104 | + (TApplicationException *) exceptionWithType: (int) type |
| 105 | reason: (NSString *) reason |
| 106 | { |
| 107 | return [[[TApplicationException alloc] initWithType: type |
| 108 | reason: reason] autorelease]; |
| 109 | } |
| 110 | |
| 111 | @end |