Show errors during loading
This commit is contained in:
@ -1,6 +1,11 @@
|
||||
import Foundation
|
||||
import AppKit
|
||||
|
||||
protocol SecurityBookmarkErrorDelegate: AnyObject {
|
||||
|
||||
func securityBookmark(error: String)
|
||||
}
|
||||
|
||||
struct SecurityBookmark {
|
||||
|
||||
enum OverwriteBehaviour {
|
||||
@ -20,6 +25,8 @@ struct SecurityBookmark {
|
||||
|
||||
private let fm = FileManager.default
|
||||
|
||||
weak var delegate: SecurityBookmarkErrorDelegate?
|
||||
|
||||
init(url: URL, isStale: Bool) {
|
||||
self.url = url
|
||||
self.isStale = isStale
|
||||
@ -31,9 +38,7 @@ struct SecurityBookmark {
|
||||
|
||||
func openFinderWindow(relativePath: String) {
|
||||
with(relativePath: relativePath) { path in
|
||||
print("Opening file at \(path)")
|
||||
NSWorkspace.shared.activateFileViewerSelecting([path])
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
@ -60,7 +65,7 @@ struct SecurityBookmark {
|
||||
do {
|
||||
data = try encoder.encode(value)
|
||||
} catch {
|
||||
print("Failed to encode \(value): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to encode \(value): \(error)")
|
||||
return false
|
||||
}
|
||||
return write(data, to: relativePath)
|
||||
@ -71,6 +76,7 @@ struct SecurityBookmark {
|
||||
createParentFolder: Bool = true,
|
||||
ifFileExists overwrite: OverwriteBehaviour = .writeIfChanged) -> Bool {
|
||||
guard let data = string.data(using: .utf8) else {
|
||||
delegate?.securityBookmark(error: "Failed to encode string to write to \(relativePath)")
|
||||
return false
|
||||
}
|
||||
return write(data, to: relativePath, createParentFolder: createParentFolder, ifFileExists: overwrite)
|
||||
@ -85,7 +91,9 @@ struct SecurityBookmark {
|
||||
|
||||
if exists(file) {
|
||||
switch overwrite {
|
||||
case .fail: return false
|
||||
case .fail:
|
||||
delegate?.securityBookmark(error: "Failed to write \(relativePath): File exists")
|
||||
return false
|
||||
case .skip: return true
|
||||
case .write: break
|
||||
case .writeIfChanged:
|
||||
@ -99,7 +107,7 @@ struct SecurityBookmark {
|
||||
try createParentIfNeeded(of: file)
|
||||
try data.write(to: file)
|
||||
} catch {
|
||||
print("Failed to write to file \(url.path()): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to write \(relativePath): \(error)")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@ -124,7 +132,11 @@ struct SecurityBookmark {
|
||||
guard let data = readData(at: relativePath) else {
|
||||
return nil
|
||||
}
|
||||
return String(data: data, encoding: .utf8)
|
||||
guard let result = String(data: data, encoding: .utf8) else {
|
||||
delegate?.securityBookmark(error: "Failed to read \(relativePath): invalid UTF-8")
|
||||
return nil
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
func readData(at relativePath: String) -> Data? {
|
||||
@ -135,7 +147,7 @@ struct SecurityBookmark {
|
||||
do {
|
||||
return try Data(contentsOf: file)
|
||||
} catch {
|
||||
print("Storage: Failed to read file \(relativePath): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to read \(relativePath) \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -148,7 +160,7 @@ struct SecurityBookmark {
|
||||
do {
|
||||
return try decoder.decode(T.self, from: data)
|
||||
} catch {
|
||||
print("Failed to decode file \(relativePath): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to decode \(relativePath): \(error)")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -163,7 +175,7 @@ struct SecurityBookmark {
|
||||
with(relativePath: relativeSource) { source in
|
||||
if !exists(source) {
|
||||
if !failIfMissing { return true }
|
||||
print("ContentScope: Could not find file \(relativeSource) to move")
|
||||
delegate?.securityBookmark(error: "Failed to move \(relativeSource): File does not exist")
|
||||
return false
|
||||
}
|
||||
|
||||
@ -171,7 +183,7 @@ struct SecurityBookmark {
|
||||
if exists(destination) {
|
||||
switch overwrite {
|
||||
case .fail:
|
||||
print("ContentScope: Could not move file \(relativeSource), file exists")
|
||||
delegate?.securityBookmark(error: "Failed to move to \(relativeDestination): File already exists")
|
||||
return false
|
||||
case .skip: return true
|
||||
case .write: break
|
||||
@ -190,7 +202,7 @@ struct SecurityBookmark {
|
||||
try fm.moveItem(at: source, to: destination)
|
||||
return true
|
||||
} catch {
|
||||
print("Failed to move \(source.path()) to \(destination.path())")
|
||||
delegate?.securityBookmark(error: "Failed to move \(source.path()) to \(destination.path()): \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -204,7 +216,9 @@ struct SecurityBookmark {
|
||||
do {
|
||||
if destination.exists {
|
||||
switch overwrite {
|
||||
case .fail: return false
|
||||
case .fail:
|
||||
delegate?.securityBookmark(error: "Failed to copy to \(relativePath): File already exists")
|
||||
return false
|
||||
case .skip: return true
|
||||
case .write: break
|
||||
case .writeIfChanged:
|
||||
@ -220,7 +234,7 @@ struct SecurityBookmark {
|
||||
try fm.copyItem(at: externalFile, to: destination)
|
||||
return true
|
||||
} catch {
|
||||
print("Failed to copy \(externalFile.path()) to \(destination.path())")
|
||||
delegate?.securityBookmark(error: "Failed to copy \(externalFile.path()) to \(relativePath): \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -229,14 +243,13 @@ struct SecurityBookmark {
|
||||
func deleteFile(at relativePath: String) -> Bool {
|
||||
with(relativePath: relativePath) { file in
|
||||
guard exists(file) else {
|
||||
print("Scope: No file to delete at \(file.path())")
|
||||
return true
|
||||
}
|
||||
do {
|
||||
try fm.removeItem(at: file)
|
||||
return true
|
||||
} catch {
|
||||
print("Failed to delete file \(file.path()): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to delete \(relativePath): \(error)")
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -291,9 +304,7 @@ struct SecurityBookmark {
|
||||
}
|
||||
|
||||
func files(inRelativeFolder relativePath: String) -> [URL]? {
|
||||
with(relativePath: relativePath) { folder in
|
||||
files(in: folder)
|
||||
}
|
||||
with(relativePath: relativePath, perform: files)
|
||||
}
|
||||
|
||||
/**
|
||||
@ -312,14 +323,13 @@ struct SecurityBookmark {
|
||||
do {
|
||||
data = try Data(contentsOf: url)
|
||||
} catch {
|
||||
#warning("Get these errors")
|
||||
print("Storage: Failed to read file \(url.path()): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to read \(url.path()): \(error)")
|
||||
return
|
||||
}
|
||||
do {
|
||||
items[id] = try decoder.decode(T.self, from: data)
|
||||
} catch {
|
||||
print("Storage: Failed to decode file \(url.path()): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to decode \(url.path()): \(error)")
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -341,7 +351,7 @@ struct SecurityBookmark {
|
||||
*/
|
||||
func perform(_ operation: (URL) -> Bool) -> Bool {
|
||||
guard url.startAccessingSecurityScopedResource() else {
|
||||
print("Failed to start security scope")
|
||||
delegate?.securityBookmark(error: "Failed to start security scope")
|
||||
return false
|
||||
}
|
||||
defer { url.stopAccessingSecurityScopedResource() }
|
||||
@ -353,7 +363,7 @@ struct SecurityBookmark {
|
||||
*/
|
||||
func perform<T>(_ operation: (URL) -> T?) -> T? {
|
||||
guard url.startAccessingSecurityScopedResource() else {
|
||||
print("Failed to start security scope")
|
||||
delegate?.securityBookmark(error: "Failed to start security scope")
|
||||
return nil
|
||||
}
|
||||
defer { url.stopAccessingSecurityScopedResource() }
|
||||
@ -367,7 +377,7 @@ struct SecurityBookmark {
|
||||
try createIfNeeded(folder)
|
||||
return true
|
||||
} catch {
|
||||
print("Failed to create folder \(folder.path())")
|
||||
delegate?.securityBookmark(error: "Failed to create folder \(folder.path())")
|
||||
return false
|
||||
}
|
||||
}
|
||||
@ -383,7 +393,7 @@ struct SecurityBookmark {
|
||||
do {
|
||||
try fm.removeItem(at: file)
|
||||
} catch {
|
||||
print("Failed to remove \(file.path()): \(error)")
|
||||
delegate?.securityBookmark(error: "Failed to delete \(file.path()): \(error)")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@ -409,7 +419,7 @@ struct SecurityBookmark {
|
||||
do {
|
||||
return try files(in: folder).filter { !$0.lastPathComponent.hasPrefix(".") }
|
||||
} catch {
|
||||
print("Failed to read list of files in \(folder.path())")
|
||||
delegate?.securityBookmark(error: "Failed to read list of files in \(folder.path())")
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user