Skip to content

Commit

Permalink
bugfix
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianKahnert committed Jan 24, 2020
1 parent ffe7a9c commit 10378f7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 27 deletions.
16 changes: 9 additions & 7 deletions Sources/chiaLib/API/Chia.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
}
Expand Down Expand Up @@ -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<T>(_ expression: @autoclosure () throws -> T, msg: String, errorTransform: (Error) -> ChiaError) throws -> T {
return try ChiaError.perform(expression()) { error in
private func perform<T>(_ 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
}
}

Expand Down
8 changes: 4 additions & 4 deletions Sources/chiaLib/API/ChiaError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
16 changes: 0 additions & 16 deletions Sources/chiaLib/Internal/Helpers/Error.swift

This file was deleted.

0 comments on commit 10378f7

Please sign in to comment.