Chris Simpson | a9b6c70 | 2018-04-08 07:11:37 -0400 | [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 | |
| 21 | /// TErrorCode |
| 22 | /// |
| 23 | /// Protocol for TError conformers' enum's to conform to. |
| 24 | /// Generic Int Thrift error code to allow error cases to have |
| 25 | /// associated values. |
| 26 | public protocol TErrorCode : CustomStringConvertible { |
| 27 | var thriftErrorCode: Int { get } |
| 28 | } |
| 29 | |
| 30 | /// TError |
| 31 | /// |
| 32 | /// Base protocol for all Thrift Error(Exception) types to conform to |
| 33 | public protocol TError : Error, CustomStringConvertible { |
| 34 | |
| 35 | /// Enum for error cases. Can be typealiased to any conforming enum |
| 36 | /// or defined nested. |
| 37 | associatedtype Code: TErrorCode |
| 38 | |
| 39 | /// Error Case, value from internal enum |
| 40 | var error: Code { get set } |
| 41 | |
| 42 | /// Optional additional message |
| 43 | var message: String? { get set } |
| 44 | |
| 45 | /// Default error case for the error type, used for generic init() |
| 46 | static var defaultCase: Code { get } |
| 47 | |
| 48 | init() |
| 49 | } |
| 50 | |
| 51 | extension TError { |
| 52 | /// Human readable description of error. Default provided for you in the |
| 53 | /// format \(Self.self): \(error.errorDescription) \n message |
| 54 | /// eg: |
| 55 | /// |
| 56 | /// TApplicationError (1): Invalid Message Type |
| 57 | /// An unknown Error has occured. |
| 58 | public var description: String { |
| 59 | var out = "\(Self.self) (\(error.thriftErrorCode)): " + error.description + "\n" |
| 60 | if let message = message { |
| 61 | out += "Message: \(message)" |
| 62 | } |
| 63 | return out |
| 64 | } |
| 65 | |
| 66 | /// Simple default Initializer for TError's |
| 67 | /// |
| 68 | /// - parameter error: ErrorCode value. Default: defaultCase |
| 69 | /// - parameter message: Custom message with error. Optional |
| 70 | /// |
| 71 | /// - returns: <#return value description#> |
| 72 | public init(error: Code, message: String? = nil) { |
| 73 | self.init() |
| 74 | self.error = error |
| 75 | self.message = message |
| 76 | } |
| 77 | } |