From 10378f73f31e90241439f768a9058ac8dd7789f4 Mon Sep 17 00:00:00 2001 From: Julian Kahnert Date: Fri, 24 Jan 2020 23:03:24 +0100 Subject: [PATCH] bugfix --- Sources/chiaLib/API/Chia.swift | 16 +++++++++------- Sources/chiaLib/API/ChiaError.swift | 8 ++++---- Sources/chiaLib/Internal/Helpers/Error.swift | 16 ---------------- 3 files changed, 13 insertions(+), 27 deletions(-) delete mode 100644 Sources/chiaLib/Internal/Helpers/Error.swift diff --git a/Sources/chiaLib/API/Chia.swift b/Sources/chiaLib/API/Chia.swift index e1936cc..d044256 100644 --- a/Sources/chiaLib/API/Chia.swift +++ b/Sources/chiaLib/API/Chia.swift @@ -66,11 +66,11 @@ public struct Chia { if let url = url { let encodedYAML = try perform(String(contentsOf: url), msg: "Config YAML could not be read.", - errorTransform: { .yamlReadingError($0) }) + error: .yamlReadingError) config = try perform(YAMLDecoder().decode(ChiaConfig.self, from: encodedYAML), msg: "YAML is not valid could not be decoded.", - errorTransform: { .yamlDecodingError($0) }) + error: .yamlDecodingError) logger?.info("Using config from: \(url.path)") } else { @@ -88,7 +88,7 @@ public struct Chia { if let appendix = config?.projectRootAppendix { projectRootFolder = try perform(Folder.current.subfolder(at: appendix), msg: "Could not find subfolder '\(appendix)'.", - errorTransform: { .projectRootNotFound($0) }) + error: .projectRootNotFound) } else { projectRootFolder = Folder.current } @@ -148,17 +148,19 @@ public struct Chia { log(results) // throw an error if one check failed with an error - this will result in an exit(1) in the - if !results.contains(where: { $0.severity == .error }) { + if results.contains(where: { $0.severity == .error }) { throw ChiaError.someChecksFailed } } // MARK: - Helper Function - private func perform(_ expression: @autoclosure () throws -> T, msg: String, errorTransform: (Error) -> ChiaError) throws -> T { - return try ChiaError.perform(expression()) { error in + private func perform(_ expression: @autoclosure () throws -> T, msg: String, error definedError: ChiaError) throws -> T { + do { + return try expression() + } catch { logger?.error(Logger.Message(extendedGraphemeClusterLiteral: msg), metadata: ["error": .string(error.localizedDescription)]) - return errorTransform(error) + throw definedError } } diff --git a/Sources/chiaLib/API/ChiaError.swift b/Sources/chiaLib/API/ChiaError.swift index ab6763d..adfe353 100644 --- a/Sources/chiaLib/API/ChiaError.swift +++ b/Sources/chiaLib/API/ChiaError.swift @@ -8,10 +8,10 @@ import Foundation /// Errors that might be thrown by `Chia`. -public enum ChiaError: Error { - case projectRootNotFound(Error) - case yamlReadingError(Error) - case yamlDecodingError(Error) +public enum ChiaError: String, Error { + case projectRootNotFound + case yamlReadingError + case yamlDecodingError case configNotFound case someChecksFailed } diff --git a/Sources/chiaLib/Internal/Helpers/Error.swift b/Sources/chiaLib/Internal/Helpers/Error.swift deleted file mode 100644 index d438247..0000000 --- a/Sources/chiaLib/Internal/Helpers/Error.swift +++ /dev/null @@ -1,16 +0,0 @@ -// -// Error.swift -// -// -// Created by Julian Kahnert on 15.01.20. -// - -extension Error { - static func perform(_ expression: @autoclosure () throws -> T, errorTransform: (Error) -> Self) throws -> T { - do { - return try expression() - } catch { - throw errorTransform(error) - } - } -}