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 "TError.h" |
| 21 | |
| 22 | |
| 23 | extern NSString *TProtocolErrorDomain; |
| 24 | |
| 25 | typedef NS_ENUM (int, TProtocolError) { |
| 26 | TProtocolErrorUnknown = 0, |
| 27 | TProtocolErrorInvalidData = 1, |
| 28 | TProtocolErrorNegativeSize = 2, |
| 29 | TProtocolErrorSizeLimit = 3, |
| 30 | TProtocolErrorBadVersion = 4, |
| 31 | TProtocolErrorNotImplemented = 5, |
| 32 | TProtocolErrorDepthLimit = 6, |
| 33 | }; |
| 34 | |
| 35 | |
| 36 | typedef NS_ENUM(int, TProtocolExtendedError) { |
| 37 | TProtocolExtendedErrorMissingRequiredField = 1001, |
| 38 | TProtocolExtendedErrorUnexpectedType = 1002, |
| 39 | TProtocolExtendedErrorMismatchedProtocol = 1003, |
| 40 | }; |
| 41 | |
| 42 | extern NSString *TProtocolErrorExtendedErrorKey; |
| 43 | extern NSString *TProtocolErrorFieldNameKey; |
| 44 | extern NSString *TProtocolErrorExpectedIdKey; |
| 45 | extern NSString *TProtocolErrorExpectedVersionKey; |
| 46 | extern NSString *TProtocolErrorTypeKey; |
| 47 | extern NSString *TProtocolErrorSourceLineKey; |
| 48 | extern NSString *TProtocolErrorSourceFileKey; |
| 49 | extern NSString *TProtocolErrorSourceMethodKey; |
| 50 | extern NSString *TProtocolErrorMessageNameKey; |
| 51 | |
| 52 | |
| 53 | #define PROTOCOL_ERROR(ret, err, ...) \ |
| 54 | if (error) { \ |
| 55 | *error = [NSError errorWithDomain:TProtocolErrorDomain \ |
| 56 | code:TProtocolError ## err \ |
| 57 | userInfo:@{NSLocalizedDescriptionKey: [NSString stringWithFormat:__VA_ARGS__], \ |
| 58 | @"SourceFile": [NSString stringWithUTF8String:__FILE__], \ |
| 59 | @"SourceLine": @(__LINE__), \ |
| 60 | @"SourceFunction": [NSString stringWithUTF8String:__PRETTY_FUNCTION__], \ |
| 61 | @"Message": self.currentMessageName ? self.currentMessageName : @""}]; \ |
| 62 | } \ |
| 63 | return ret |
| 64 | |
| 65 | #define PROTOCOL_TRANSPORT_ERROR(ret, errorPtr, ...) \ |
| 66 | if (errorPtr) { \ |
| 67 | *error = [NSError errorWithDomain:TProtocolErrorDomain \ |
| 68 | code:TProtocolErrorUnknown \ |
| 69 | userInfo:@{NSLocalizedDescriptionKey: [[NSString stringWithFormat:__VA_ARGS__] stringByAppendingFormat:@": %@", [(*errorPtr) localizedDescription]], \ |
| 70 | TProtocolErrorSourceFileKey: [NSString stringWithUTF8String:__FILE__], \ |
| 71 | TProtocolErrorSourceLineKey: @(__LINE__), \ |
| 72 | TProtocolErrorSourceMethodKey: [NSString stringWithUTF8String:__PRETTY_FUNCTION__], \ |
| 73 | TProtocolErrorMessageNameKey: self.currentMessageName ? self.currentMessageName : @"", \ |
| 74 | NSUnderlyingErrorKey: *errorPtr}]; \ |
| 75 | } \ |
| 76 | return ret |