Improve error printing

Improve path printing

Update comment
This commit is contained in:
Christoph Hagen 2022-08-25 00:09:39 +02:00
parent 06daa5e5fa
commit 2e6542225f
3 changed files with 31 additions and 16 deletions

View File

@ -130,8 +130,8 @@ struct Element {
self.inputFolder = folder self.inputFolder = folder
self.path = "" self.path = ""
let source = "root" let source = GenericMetadata.metadataFileName
guard let metadata = try GenericMetadata(path: nil, with: context) else { guard let metadata = try GenericMetadata(source: source, with: context) else {
return nil return nil
} }
@ -166,16 +166,17 @@ struct Element {
} }
self.elements = try subFolders.compactMap { subFolder in self.elements = try subFolders.compactMap { subFolder in
let s = (source.unwrapped { $0 + "/" } ?? "") + subFolder.lastPathComponent let s = (source.unwrapped { $0 + "/" } ?? "") + subFolder.lastPathComponent
return try Element(parent: self, folder: subFolder, with: context, source: s) return try Element(parent: self, folder: subFolder, with: context, path: s)
} }
} }
init?(parent: Element, folder: URL, with: Context, source: String) throws { init?(parent: Element, folder: URL, with: Context, path: String) throws {
let validation = context.validation let validation = context.validation
self.inputFolder = folder self.inputFolder = folder
self.path = source self.path = path
guard let metadata = try GenericMetadata(path: source, with: context) else { let source = path + "/" + GenericMetadata.metadataFileName
guard let metadata = try GenericMetadata(source: source, with: context) else {
return nil return nil
} }
self.author = metadata.author ?? parent.author self.author = metadata.author ?? parent.author
@ -203,7 +204,7 @@ struct Element {
// TODO: Propagate external files from the parent if subpath matches? // TODO: Propagate external files from the parent if subpath matches?
self.externalFiles = metadata.externalFiles ?? [] self.externalFiles = metadata.externalFiles ?? []
self.requiredFiles = metadata.requiredFiles ?? [] self.requiredFiles = metadata.requiredFiles ?? []
self.thumbnailStyle = validation.thumbnailStyle(metadata.state, source: source) self.thumbnailStyle = validation.thumbnailStyle(metadata.thumbnailStyle, source: source)
self.useManualSorting = metadata.useManualSorting ?? false self.useManualSorting = metadata.useManualSorting ?? false
self.overviewItemCount = metadata.overviewItemCount ?? parent.overviewItemCount self.overviewItemCount = metadata.overviewItemCount ?? parent.overviewItemCount
self.languages = parent.languages.compactMap { parentData in self.languages = parent.languages.compactMap { parentData in
@ -222,7 +223,7 @@ struct Element {
.forEach { .forEach {
validation.add(warning: "Language '\($0)' not found in parent, so not generated", source: source) validation.add(warning: "Language '\($0)' not found in parent, so not generated", source: source)
} }
try self.readElements(in: folder, source: source, with: context) try self.readElements(in: folder, source: path, with: context)
} }
} }

View File

@ -2,12 +2,28 @@ import Foundation
final class ErrorOutput { final class ErrorOutput {
private enum LogLevel: String {
case error = "ERROR"
case warning = "WARNING"
case info = "INFO"
}
init() { init() {
} }
private func add(_ type: LogLevel, item: ContentError) {
let errorText: String
if let err = item.error {
errorText = ", Error: \(err.localizedDescription)"
} else {
errorText = ""
}
print("[\(type.rawValue)] \(item.reason), Source: \(item.source)\(errorText)")
}
func add(error: ContentError) { func add(error: ContentError) {
print("[ERROR] Reason: \(error.reason), Source: \(error.source), Error: \(error.error?.localizedDescription ?? "nil")") add(.error, item: error)
} }
func add(error reason: String, source: String, error: Error? = nil) { func add(error reason: String, source: String, error: Error? = nil) {
@ -15,7 +31,7 @@ final class ErrorOutput {
} }
func add(warning: ContentError) { func add(warning: ContentError) {
print("[WARNING] Reason: \(warning.reason), Source: \(warning.source), Error: \(warning.error?.localizedDescription ?? "nil")") add(.warning, item: warning)
} }
func add(warning reason: String, source: String, error: Error? = nil) { func add(warning reason: String, source: String, error: Error? = nil) {
@ -23,7 +39,7 @@ final class ErrorOutput {
} }
func add(info: ContentError) { func add(info: ContentError) {
print("[INFO] Reason: \(info.reason), Source: \(info.source), Error: \(info.error?.localizedDescription ?? "nil")") add(.info, item: info)
} }
func add(info reason: String, source: String, error: Error? = nil) { func add(info reason: String, source: String, error: Error? = nil) {

View File

@ -136,14 +136,12 @@ extension GenericMetadata {
/** /**
Decode metadata in a folder. Decode metadata in a folder.
- Parameter path: The path to the element folder, relative to the source root - Parameter source: The path to the metadata file, relative to the source root
- Parameter context: The context for the element (validation, file access, etc.) - Parameter context: The context for the element (validation, file access, etc.)
- Note: The decoding routine also checks for unknown properties, and writes them to the output. - Note: The decoding routine also checks for unknown properties, and writes them to the output.
*/ */
init?(path: String?, with context: Context) throws { init?(source: String, with context: Context) throws {
let source = path ?? "root" guard let data = try context.fileSystem.loadDataContent(inputPath: source) else {
let metadataPath = (path.unwrapped { $0 + "/" } ?? "") + GenericMetadata.metadataFileName
guard let data = try context.fileSystem.loadDataContent(inputPath: metadataPath) else {
return nil return nil
} }