Jens Geyer | 56e5b9b | 2015-10-09 22:01:55 +0200 | [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 | |
| 20 | #import "TApplicationError.h" |
| 21 | #import "TProtocolUtil.h" |
| 22 | |
| 23 | |
| 24 | NSString *TApplicationErrorDomain = @"TApplicationErrorDomain"; |
| 25 | |
| 26 | |
| 27 | NSString *TApplicationErrorNameKey = @"name"; |
| 28 | NSString *TApplicationErrorReasonKey = @"reason"; |
| 29 | NSString *TApplicationErrorMethodKey = @"method"; |
| 30 | |
| 31 | |
| 32 | @implementation NSError (TApplicationError) |
| 33 | |
| 34 | -(NSString *) reason |
| 35 | { |
| 36 | return self.userInfo[TApplicationErrorReasonKey]; |
| 37 | } |
| 38 | |
| 39 | -(NSString *) name |
| 40 | { |
| 41 | return self.userInfo[TApplicationErrorNameKey]; |
| 42 | } |
| 43 | |
| 44 | +(instancetype) errorWithType:(TApplicationError)type reason:(NSString *)reason |
| 45 | { |
| 46 | NSString *name; |
| 47 | switch (type) { |
| 48 | case TApplicationErrorUnknownMethod: |
| 49 | name = @"Unknown Method"; |
| 50 | break; |
| 51 | |
| 52 | case TApplicationErrorInvalidMessageType: |
| 53 | name = @"Invalid Message Type"; |
| 54 | break; |
| 55 | |
| 56 | case TApplicationErrorWrongMethodName: |
| 57 | name = @"Wrong Method Name"; |
| 58 | break; |
| 59 | |
| 60 | case TApplicationErrorBadSequenceId: |
| 61 | name = @"Bad Sequence ID"; |
| 62 | break; |
| 63 | |
| 64 | case TApplicationErrorMissingResult: |
| 65 | name = @"Missing Result"; |
| 66 | break; |
| 67 | |
| 68 | case TApplicationErrorInternalError: |
| 69 | name = @"Internal Error"; |
| 70 | break; |
| 71 | |
| 72 | case TApplicationErrorProtocolError: |
| 73 | name = @"Protocol Error"; |
| 74 | break; |
| 75 | |
| 76 | case TApplicationErrorInvalidTransform: |
| 77 | name = @"Invalid Transform"; |
| 78 | break; |
| 79 | |
| 80 | case TApplicationErrorInvalidProtocol: |
| 81 | name = @"Invalid Protocol"; |
| 82 | break; |
| 83 | |
| 84 | case TApplicationErrorUnsupportedClientType: |
| 85 | name = @"Unsupported Client Type"; |
| 86 | break; |
| 87 | |
| 88 | default: |
| 89 | name = @"Unknown"; |
| 90 | break; |
| 91 | } |
| 92 | |
| 93 | NSDictionary *userInfo; |
| 94 | if (reason) { |
| 95 | userInfo = @{TApplicationErrorNameKey:name, |
| 96 | TApplicationErrorReasonKey:reason}; |
| 97 | } |
| 98 | else { |
| 99 | userInfo = @{TApplicationErrorNameKey:name}; |
| 100 | } |
| 101 | |
| 102 | return [NSError errorWithDomain:TApplicationErrorDomain |
| 103 | code:type |
| 104 | userInfo:userInfo]; |
| 105 | } |
| 106 | |
| 107 | |
| 108 | +(instancetype) read:(id<TProtocol>)protocol |
| 109 | { |
| 110 | NSString *reason = nil; |
| 111 | SInt32 type = TApplicationErrorUnknown; |
| 112 | SInt32 fieldType; |
| 113 | SInt32 fieldID; |
| 114 | |
| 115 | NSError *error; |
| 116 | if (![protocol readStructBeginReturningName:NULL error:&error]) { |
| 117 | return error; |
| 118 | } |
| 119 | |
| 120 | while (true) { |
| 121 | |
| 122 | if (![protocol readFieldBeginReturningName:NULL |
| 123 | type:&fieldType |
| 124 | fieldID:&fieldID |
| 125 | error:&error]) |
| 126 | { |
| 127 | return error; |
| 128 | } |
| 129 | |
| 130 | if (fieldType == TTypeSTOP) { |
| 131 | break; |
| 132 | } |
| 133 | |
| 134 | switch (fieldID) { |
| 135 | case 1: |
| 136 | if (fieldType == TTypeSTRING) { |
| 137 | if (![protocol readString:&reason error:&error]) { |
| 138 | return error; |
| 139 | } |
| 140 | } |
| 141 | else { |
| 142 | if (![TProtocolUtil skipType:fieldType onProtocol:protocol error:&error]) { |
| 143 | return error; |
| 144 | } |
| 145 | } |
| 146 | break; |
| 147 | |
| 148 | case 2: |
| 149 | if (fieldType == TTypeI32) { |
| 150 | if (![protocol readI32:&type error:&error]) { |
| 151 | return error; |
| 152 | } |
| 153 | } |
| 154 | else { |
| 155 | if (![TProtocolUtil skipType:fieldType onProtocol:protocol error:&error]) { |
| 156 | return error; |
| 157 | } |
| 158 | } |
| 159 | break; |
| 160 | |
| 161 | default: |
| 162 | if (![TProtocolUtil skipType:fieldType onProtocol:protocol error:&error]) { |
| 163 | return error; |
| 164 | } |
| 165 | break; |
| 166 | } |
| 167 | if (![protocol readFieldEnd:&error]) { |
| 168 | return error; |
| 169 | } |
| 170 | |
| 171 | } |
| 172 | |
| 173 | if (![protocol readStructEnd:&error]) { |
| 174 | return error; |
| 175 | } |
| 176 | |
| 177 | return [NSError errorWithType:type reason:reason]; |
| 178 | } |
| 179 | |
| 180 | |
| 181 | -(BOOL) write:(id<TProtocol>)protocol error:(NSError *__autoreleasing *)error |
| 182 | { |
| 183 | if (![protocol writeStructBeginWithName:@"TApplicationException" error:error]) { |
| 184 | return NO; |
| 185 | } |
| 186 | |
| 187 | if (self.localizedDescription != nil) { |
| 188 | if (![protocol writeFieldBeginWithName:@"message" |
| 189 | type:TTypeSTRING |
| 190 | fieldID:1 error:error]) |
| 191 | { |
| 192 | return NO; |
| 193 | } |
| 194 | |
| 195 | if (![protocol writeString:self.localizedDescription error:error]) { |
| 196 | return NO; |
| 197 | } |
| 198 | |
| 199 | if (![protocol writeFieldEnd:error]) { |
| 200 | return NO; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if (![protocol writeFieldBeginWithName:@"type" |
| 205 | type:TTypeI32 |
| 206 | fieldID:2 |
| 207 | error:error]) |
| 208 | { |
| 209 | return NO; |
| 210 | } |
| 211 | |
| 212 | if (![protocol writeI32:(SInt32)self.code error:error]) { |
| 213 | return NO; |
| 214 | } |
| 215 | |
| 216 | if (![protocol writeFieldEnd:error]) { |
| 217 | return NO; |
| 218 | } |
| 219 | |
| 220 | if (![protocol writeFieldStop:error]) { |
| 221 | return NO; |
| 222 | } |
| 223 | |
| 224 | if (![protocol writeStructEnd:error]) { |
| 225 | return NO; |
| 226 | } |
| 227 | |
| 228 | return YES; |
| 229 | } |
| 230 | |
| 231 | @end |