From dcfa9473e955e372d2c32d7fc682a6fa9ff30bdf Mon Sep 17 00:00:00 2001 From: Roopesh Chander Date: Thu, 6 Dec 2018 19:05:46 +0530 Subject: Error handling: Use WireGuardAppError and WireGuardResult throughout the app Signed-off-by: Roopesh Chander --- WireGuard/WireGuard/ZipArchive/ZipArchive.swift | 13 ++++++++++++- WireGuard/WireGuard/ZipArchive/ZipExporter.swift | 18 ++++++++++++++---- WireGuard/WireGuard/ZipArchive/ZipImporter.swift | 19 ++++++++++++++----- 3 files changed, 40 insertions(+), 10 deletions(-) (limited to 'WireGuard/WireGuard/ZipArchive') diff --git a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift index d907803..2faa516 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipArchive.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipArchive.swift @@ -3,10 +3,21 @@ import Foundation -enum ZipArchiveError: Error { +enum ZipArchiveError: WireGuardAppError { case cantOpenInputZipFile case cantOpenOutputZipFileForWriting case badArchive + + func alertText() -> (String, String) { + switch (self) { + case .cantOpenInputZipFile: + return ("Unable to read zip archive", "The zip archive could not be read.") + case .cantOpenOutputZipFileForWriting: + return ("Unable to create zip archive", "Could not open zip file for writing.") + case .badArchive: + return ("Unable to read zip archive", "Bad or corrupt zip archive.") + } + } } class ZipArchive { diff --git a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift index bd6c3dd..a4e9d7a 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipExporter.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipExporter.swift @@ -3,12 +3,20 @@ import UIKit -enum ZipExporterError: Error { +enum ZipExporterError: WireGuardAppError { case noTunnelsToExport + + func alertText() -> (String, String) { + switch (self) { + case .noTunnelsToExport: + return ("Nothing to export", "There are no tunnels to export") + } + } } class ZipExporter { - static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to url: URL, completion: @escaping (Error?) -> Void) { + static func exportConfigFiles(tunnelConfigurations: [TunnelConfiguration], to url: URL, + completion: @escaping (WireGuardAppError?) -> Void) { guard (!tunnelConfigurations.isEmpty) else { completion(ZipExporterError.noTunnelsToExport) @@ -27,9 +35,11 @@ class ZipExporter { } do { try ZipArchive.archive(inputs: inputsToArchiver, to: url) - } catch (let e) { - DispatchQueue.main.async { completion(e) } + } catch (let error as WireGuardAppError) { + DispatchQueue.main.async { completion(error) } return + } catch { + fatalError() } DispatchQueue.main.async { completion(nil) } } diff --git a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift index 4c439d4..507a823 100644 --- a/WireGuard/WireGuard/ZipArchive/ZipImporter.swift +++ b/WireGuard/WireGuard/ZipArchive/ZipImporter.swift @@ -3,12 +3,19 @@ import UIKit -enum ZipImporterError: Error { +enum ZipImporterError: WireGuardAppError { case noTunnelsInZipArchive + + func alertText() -> (String, String) { + switch (self) { + case .noTunnelsInZipArchive: + return ("No tunnels in zip archive", "No .conf tunnel files were found inside the zip archive.") + } + } } class ZipImporter { - static func importConfigFiles(from url: URL, completion: @escaping ([TunnelConfiguration?], Error?) -> Void) { + static func importConfigFiles(from url: URL, completion: @escaping (WireGuardResult<[TunnelConfiguration?]>) -> Void) { DispatchQueue.global(qos: .userInitiated).async { var unarchivedFiles: [(fileName: String, contents: Data)] do { @@ -26,9 +33,11 @@ class ZipImporter { if (unarchivedFiles.isEmpty) { throw ZipImporterError.noTunnelsInZipArchive } - } catch (let error) { - DispatchQueue.main.async { completion([], error) } + } catch (let error as WireGuardAppError) { + DispatchQueue.main.async { completion(.failure(error)) } return + } catch { + fatalError() } unarchivedFiles.sort { $0.fileName < $1.fileName } @@ -45,7 +54,7 @@ class ZipImporter { } configs[i] = tunnelConfig } - DispatchQueue.main.async { completion(configs, nil) } + DispatchQueue.main.async { completion(.success(configs)) } } } } -- cgit v1.2.3-59-g8ed1b