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; |
Roger Meier | 0193149 | 2012-12-22 21:31:03 +0100 | [diff] [blame] | 48 | 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 Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 63 | 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 Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 80 | [protocol readStructBeginReturningName: NULL]; |
| 81 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 82 | while (true) { |
| 83 | [protocol readFieldBeginReturningName: NULL |
| 84 | type: &fieldType |
| 85 | fieldID: &fieldID]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 86 | if (fieldType == TType_STOP) { |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 87 | break; |
| 88 | } |
| 89 | switch (fieldID) { |
| 90 | case 1: |
| 91 | if (fieldType == TType_STRING) { |
| 92 | reason = [protocol readString]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 93 | } else { |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 94 | [TProtocolUtil skipType: fieldType onProtocol: protocol]; |
| 95 | } |
| 96 | break; |
| 97 | case 2: |
| 98 | if (fieldType == TType_I32) { |
| 99 | type = [protocol readI32]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 100 | } else { |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 101 | [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 Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 116 | - (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 Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 139 | + (TApplicationException *) exceptionWithType: (int) type |
| 140 | reason: (NSString *) reason |
| 141 | { |
| 142 | return [[[TApplicationException alloc] initWithType: type |
Jake Farrell | 9689d89 | 2011-12-06 01:07:17 +0000 | [diff] [blame] | 143 | reason: reason] autorelease_stub]; |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | @end |