|
| 1 | +// |
| 2 | +// Created by Ettore Pasquini on 3/13/24. |
| 3 | +// Copyright © 2024 The New York Public Library. All Rights Reserved. |
| 4 | +// |
| 5 | + |
| 6 | +/// A protocol that all Error subclass should be extended to support |
| 7 | +/// inside the NYPL eReader. |
| 8 | +/// |
| 9 | +/// - Important: different errors may have the same user friendly |
| 10 | +/// description but different underlying causes and therefore different |
| 11 | +/// developer friendly descriptions and errorIDs. |
| 12 | +public protocol FriendlyError: Error { |
| 13 | + |
| 14 | + /// A localized description of the error that will be understandable by |
| 15 | + /// most users of the app. |
| 16 | + var userFriendlyMessage: String {get} |
| 17 | + |
| 18 | + /// A very short lowercase string common to related errors. |
| 19 | + var domain: String {get} |
| 20 | + |
| 21 | + /// A very short lowercase string identifying the error within the `domain`. |
| 22 | + var code: String {get} |
| 23 | + |
| 24 | + // MARK: Properties with default implementations |
| 25 | + |
| 26 | + /// A localized description of the error that will be understandable by |
| 27 | + /// most users of the app and that provides the `errorID`. |
| 28 | + /// |
| 29 | + /// **Default implementation provided.** |
| 30 | + /// |
| 31 | + /// - Important: there's a 1-many relation between this and |
| 32 | + /// `devFriendlyDescription` / `errorID`. |
| 33 | + var userFriendlyDescription: String {get} |
| 34 | + |
| 35 | + /// A detailed description of the error in English. |
| 36 | + /// |
| 37 | + /// **Default implementation provided.** |
| 38 | + /// |
| 39 | + /// - Important: there's a 1-1 relation between this and `errorID`. |
| 40 | + var devFriendlyDescription: String {get} |
| 41 | + |
| 42 | + /// A short identifier of the error that may be displayed to the user. |
| 43 | + /// This error ID should be unique across server, web client, native client. |
| 44 | + /// |
| 45 | + /// **Default implementation provided.** |
| 46 | + /// |
| 47 | + /// This MUST include the `domain` string + a code unique to the domain. |
| 48 | + /// This combination will help us identify the underlying cause of a |
| 49 | + /// problem reported by a user. |
| 50 | + var errorID: String {get} |
| 51 | +} |
| 52 | + |
| 53 | +// MARK: - Default Implementations |
| 54 | + |
| 55 | +public extension FriendlyError { |
| 56 | + /// Default implementation. |
| 57 | + /// |
| 58 | + /// This default implementation provides, in order: |
| 59 | + /// 1. The localized message to the user; |
| 60 | + /// 2. `The errorID` that will help identify/debug the error. |
| 61 | + var userFriendlyDescription: String { |
| 62 | + "\(userFriendlyMessage) [\(errorID)]" |
| 63 | + } |
| 64 | + |
| 65 | + /// Default implementation. |
| 66 | + /// |
| 67 | + /// This default implementation provides, in order: |
| 68 | + /// 1. The type of the error |
| 69 | + /// 2. String interpolation of `self` (doesn't seem to include the type, |
| 70 | + /// but it does include interpolation of nested errors, if present) |
| 71 | + /// 3. `The errorID` |
| 72 | + /// 4. The system `localizedDescription` of the error. This is sometimes |
| 73 | + /// redundant but it's provided as additional context. |
| 74 | + var devFriendlyDescription: String { |
| 75 | + "[\(type(of: self)): \(self)] (\(errorID)) \(localizedDescription)" |
| 76 | + } |
| 77 | + |
| 78 | + /// Default implementation. |
| 79 | + /// |
| 80 | + /// This default implementation provides the error domain and code. |
| 81 | + var errorID: String { |
| 82 | + "\(domain): \(code)" |
| 83 | + } |
| 84 | +} |
0 commit comments