| David Reiss | ea2cba8 | 2009-03-30 21:35:00 +0000 | [diff] [blame] | 1 | /* | 
|  | 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 Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 20 | #import "TApplicationException.h" | 
|  | 21 | #import "TProtocolUtil.h" | 
| Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 22 | #import "TObjective-C.h" | 
| Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 23 |  | 
|  | 24 | @implementation TApplicationException | 
|  | 25 |  | 
|  | 26 | - (id) initWithType: (int) type | 
|  | 27 | reason: (NSString *) reason | 
|  | 28 | { | 
| Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 29 | mType = type; | 
|  | 30 |  | 
| Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 31 | 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 Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 65 | [protocol readStructBeginReturningName: NULL]; | 
|  | 66 |  | 
| Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 67 | while (true) { | 
|  | 68 | [protocol readFieldBeginReturningName: NULL | 
|  | 69 | type: &fieldType | 
|  | 70 | fieldID: &fieldID]; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 71 | if (fieldType == TType_STOP) { | 
| Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 72 | break; | 
|  | 73 | } | 
|  | 74 | switch (fieldID) { | 
|  | 75 | case 1: | 
|  | 76 | if (fieldType == TType_STRING) { | 
|  | 77 | reason = [protocol readString]; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 78 | } else { | 
| Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 79 | [TProtocolUtil skipType: fieldType onProtocol: protocol]; | 
|  | 80 | } | 
|  | 81 | break; | 
|  | 82 | case 2: | 
|  | 83 | if (fieldType == TType_I32) { | 
|  | 84 | type = [protocol readI32]; | 
| David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 85 | } else { | 
| Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 86 | [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 Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 101 | - (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 Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 124 | + (TApplicationException *) exceptionWithType: (int) type | 
|  | 125 | reason: (NSString *) reason | 
|  | 126 | { | 
|  | 127 | return [[[TApplicationException alloc] initWithType: type | 
| Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 128 | reason: reason] autorelease_stub]; | 
| Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 129 | } | 
|  | 130 |  | 
|  | 131 | @end |