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" |
| 22 | |
| 23 | @implementation TApplicationException |
| 24 | |
| 25 | - (id) initWithType: (int) type |
| 26 | reason: (NSString *) reason |
| 27 | { |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 28 | mType = type; |
| 29 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 30 | NSString * name; |
| 31 | switch (type) { |
| 32 | case TApplicationException_UNKNOWN_METHOD: |
| 33 | name = @"Unknown method"; |
| 34 | break; |
| 35 | case TApplicationException_INVALID_MESSAGE_TYPE: |
| 36 | name = @"Invalid message type"; |
| 37 | break; |
| 38 | case TApplicationException_WRONG_METHOD_NAME: |
| 39 | name = @"Wrong method name"; |
| 40 | break; |
| 41 | case TApplicationException_BAD_SEQUENCE_ID: |
| 42 | name = @"Bad sequence ID"; |
| 43 | break; |
| 44 | case TApplicationException_MISSING_RESULT: |
| 45 | name = @"Missing result"; |
| 46 | break; |
| 47 | default: |
| 48 | name = @"Unknown"; |
| 49 | break; |
| 50 | } |
| 51 | |
| 52 | self = [super initWithName: name reason: reason userInfo: nil]; |
| 53 | return self; |
| 54 | } |
| 55 | |
| 56 | |
| 57 | + (TApplicationException *) read: (id <TProtocol>) protocol |
| 58 | { |
| 59 | NSString * reason = nil; |
| 60 | int type = TApplicationException_UNKNOWN; |
| 61 | int fieldType; |
| 62 | int fieldID; |
| 63 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 64 | [protocol readStructBeginReturningName: NULL]; |
| 65 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 66 | while (true) { |
| 67 | [protocol readFieldBeginReturningName: NULL |
| 68 | type: &fieldType |
| 69 | fieldID: &fieldID]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 70 | if (fieldType == TType_STOP) { |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 71 | break; |
| 72 | } |
| 73 | switch (fieldID) { |
| 74 | case 1: |
| 75 | if (fieldType == TType_STRING) { |
| 76 | reason = [protocol readString]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 77 | } else { |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 78 | [TProtocolUtil skipType: fieldType onProtocol: protocol]; |
| 79 | } |
| 80 | break; |
| 81 | case 2: |
| 82 | if (fieldType == TType_I32) { |
| 83 | type = [protocol readI32]; |
David Reiss | 0c90f6f | 2008-02-06 22:18:40 +0000 | [diff] [blame] | 84 | } else { |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 85 | [TProtocolUtil skipType: fieldType onProtocol: protocol]; |
| 86 | } |
| 87 | break; |
| 88 | default: |
| 89 | [TProtocolUtil skipType: fieldType onProtocol: protocol]; |
| 90 | break; |
| 91 | } |
| 92 | [protocol readFieldEnd]; |
| 93 | } |
| 94 | [protocol readStructEnd]; |
| 95 | |
| 96 | return [TApplicationException exceptionWithType: type reason: reason]; |
| 97 | } |
| 98 | |
| 99 | |
Mark Slee | 8440605 | 2007-11-20 01:39:25 +0000 | [diff] [blame] | 100 | - (void) write: (id <TProtocol>) protocol |
| 101 | { |
| 102 | [protocol writeStructBeginWithName: @"TApplicationException"]; |
| 103 | |
| 104 | if ([self reason] != nil) { |
| 105 | [protocol writeFieldBeginWithName: @"message" |
| 106 | type: TType_STRING |
| 107 | fieldID: 1]; |
| 108 | [protocol writeString: [self reason]]; |
| 109 | [protocol writeFieldEnd]; |
| 110 | } |
| 111 | |
| 112 | [protocol writeFieldBeginWithName: @"type" |
| 113 | type: TType_I32 |
| 114 | fieldID: 2]; |
| 115 | [protocol writeI32: mType]; |
| 116 | [protocol writeFieldEnd]; |
| 117 | |
| 118 | [protocol writeFieldStop]; |
| 119 | [protocol writeStructEnd]; |
| 120 | } |
| 121 | |
| 122 | |
Mark Slee | 2ac60ed | 2007-09-19 21:10:18 +0000 | [diff] [blame] | 123 | + (TApplicationException *) exceptionWithType: (int) type |
| 124 | reason: (NSString *) reason |
| 125 | { |
| 126 | return [[[TApplicationException alloc] initWithType: type |
| 127 | reason: reason] autorelease]; |
| 128 | } |
| 129 | |
| 130 | @end |